Easy php login/signup script

Discussion in 'PHP' started by Xifyhosting, Sep 20, 2011.

  1. #1
    Well i'm learning php and this is the first major thing i have done. If you find any error i'm happy for suggestions to fix it.

    First create a database in cpanel with username and password like you would normally.

    Go to PHPmyadmin ( i will update this later to the php way to create the database)

    Create a table members Number of fields 13:

    [​IMG]
    [​IMG]
    [​IMG]
    [​IMG]



    Coding:
    Index.php
    <? 
    session_start();
    if(isset($_SESSION['username'])){
    echo $_SESSION["username"];
    }
    else
    {
    ?>
    <a href="http://mystatsviewer.com/signup.php"> Sign up</a>
    <a href="http://mystatsviewer.com/login.php"> Login</a>
    <?php
    }
    ?>
    PHP:
    Signup.php:
    <script language="javascript"> 
      function DoSubmit ()
      {
     
        if (document.form1.username.value == "") {
          alert ("NO username Entered!");
          document.form1.username.focus();
          return "";
          }
         if (document.form1.password.value == "") {
          alert ("NO Password Entered!");
          document.form1.password.focus();
          return "";
          }
         if (document.form1.Email.value == "") {
          alert ("NO E-mail Entered!");
          document.form1.Email.focus();
          return "";
        }
     
        document.form1.submit();
      }
    </script>
    
    <?php
    /* Add the database */
    include("include/config.php");
    mysql_connect($host, $dbase_user, $dbase_pass);
    @mysql_select_db($db_name) or die( "No connection");
    /* Check if already signed up */
    
    /* collect form information -password */
    $username = mysql_real_escape_string($_POST['username']);
    $email = mysql_real_escape_string($_POST['Email']);
    $gamertag = mysql_real_escape_string($_POST['Gamertag']);
    $aim = mysql_real_escape_string($_POST['aim']);
    $msn = mysql_real_escape_string($_POST['msn']);
    $Location = mysql_real_escape_string($_POST['Location']);
    $Website = mysql_real_escape_string($_POST['Website']);
    $ip = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
    
    /* Password to encyrpt */
    $pass = mysql_real_escape_string($_POST['password']);
    $password = md5(sha1($pass));
    if($username == "" || $email == "" || $password == "")
    {
    ?>
    <form name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> 
    Username: <input type="text" name="username" /> 
    Password: <input type="text" name="password" />
    E-mail: <input type="text" name="Email" />
    Gamertag: <input type="text" name="Gamertag" />  
    AIM: <input type="text" name="aim" /> 
    MSN: <input type="text" name="msn" /> 
    Location: <input type="text" name="Location" /> 
    Website: <input type="text" name="website" /> 
    <input type="button" onclick="DoSubmit ()" value="Submit"> 
    </form>
    <?php
    } else {
    $result = mysql_query("SELECT * FROM `members` WHERE `username` = '$username'")
     or die(mysql_error()); 
    
    if(mysql_num_rows($result)!==0)
    {
    echo "username already exists"; 
    ?>
    <form name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> 
    Username: <input type="text" name="username" /> 
    Password: <input type="text" name="password" />
    E-mail: <input type="text" name="Email" />
    Gamertag: <input type="text" name="Gamertag" />  
    AIM: <input type="text" name="aim" /> 
    MSN: <input type="text" name="msn" /> 
    Location: <input type="text" name="Location" /> 
    Website: <input type="text" name="website" /> 
    <input type="button" onclick="DoSubmit();" value="Submit"> 
    </form>
    <?php
    }
    
    else {
        $result2 = mysql_query("SELECT * FROM `members` WHERE `E-mail` = '$email'")
     or die(mysql_error()); 
     
     if(mysql_num_rows($result2)!==0)
     {
    ?>
    <form name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> 
    Username: <input type="text" name="username" /> 
    Password: <input type="text" name="password" />
    echo "E-mail already exists"; 
    E-mail: <input type="text" name="Email" />
    Gamertag: <input type="text" name="Gamertag" />  
    AIM: <input type="text" name="aim" /> 
    MSN: <input type="text" name="msn" /> 
    Location: <input type="text" name="Location" /> 
    Website: <input type="text" name="website" /> 
    <input type="button" onclick="DoSubmit();" value="Submit"> 
    </form>
    <?php
    }
     
     else {
    $result3 = mysql_query("SELECT * FROM `members` WHERE `ip` = '$ip'")
     or die(mysql_error()); 
     
     if(mysql_num_rows($result3)!==0)
      {
      echo "Your IP already signed up"; 
    ?>
    <form name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> 
    Username: <input type="text" name="username" /> 
    Password: <input type="text" name="password" />
    E-mail: <input type="text" name="Email" />
    Gamertag: <input type="text" name="Gamertag" />  
    AIM: <input type="text" name="aim" /> 
    MSN: <input type="text" name="msn" /> 
    Location: <input type="text" name="Location" /> 
    Website: <input type="text" name="website" /> 
    <input type="button" onclick="DoSubmit ()" value="Submit"> 
    </form>
    <?php
    } 
    else {
    mysql_query("INSERT INTO members (`username`,`password`,`E-mail`,`Joined`,`Group`,`Last_Activate`,`IP`,`Gamertag`,`AIM`,`MSN`,`Website`,`Location`)  VALUES('$_POST[username]','$password','$_POST[Email]','".time()."','Member','".time()."','$ip','$_POST[Gamertag]','$_POST[aim]','$_POST[msn]','$_POST[Website]','$_POST[Location]')") 
    or die(mysql_error());  
    
    echo " Thank you for signing up ".$username;
    }
    }
    }
    }
    ?>
    PHP:
    Login.php
    <?php session_start(); ?>
    <head>
    <script language="javascript"> 
      function DoSubmit ()
      {
     
        if (document.form1.username.value == "") {
          alert ("NO username Entered!");
          document.form1.username.focus();
          return "";
          }
         if (document.form1.password.value == "") {
          alert ("NO Password Entered!");
          document.form1.password.focus();
          return "";
          }
     
        document.form1.submit();
      }
    </script>
    </head>
    
    <body>
    <?php
    
    /* Add the database */
    include("include/config.php");
    
    mysql_connect($host, $dbase_user, $dbase_pass);
    @mysql_select_db($db_name) or die( "No connection");
    /* Check if already signed up */
    /* collect form information -password */
    $username = mysql_real_escape_string($_POST['username']);
    /* Password to encyrpt */
    $pass = mysql_real_escape_string($_POST['password']);
    $password = md5(sha1($pass));
    if($username == "" || $password == "")
    {
    ?>
    <form name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> 
    Username: <input type="text" name="username" /> 
    Password: <input type="text" name="password" />
    <input type="button" onclick="DoSubmit ()" value="Submit"> 
    </form>
    <?php
    } else {
    $result = mysql_query("SELECT * FROM `members` WHERE `username` = '$username' AND `password` = '$password'")
     or die(mysql_error()); 
    
    if(mysql_num_rows($result)!==1)
    {
    ?>
    <form name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> 
    Invald username or password
    Username: <input type="text" name="username" /> 
    Password: <input type="text" name="password" />
    <input type="button" onclick="DoSubmit ()" value="Submit"> 
    </form>
    <?php
    } Else {
    echo " Thank you for logging in ".$username;
    $_SESSION['username']=$username;
    ?>
    <a href="http://mystatsviewer.com/">Home Page</a>
    <?php
    }
    
    }
    
    ?>
    </body>
    PHP:
    logout.php:
    <?php
    session_start(); 
    session_destroy();
    ?>
    PHP:
    Now create a folder and call it include. now add config.php
    config.php:
    <?php
    $host = "localhost"; //SQL host normally localhost
    $dbase_user = "nukezill_login"; // database username
    $dbase_pass = "1521"; // database password
    $db_name = "nukezill_login"; // Database name
    $dbase_pre = ""; // table prefix if apply
    
    mysql_connect(localhost, $dbase_user, $dbase_pass);
    @mysql_select_db($db_name) or die( "No connection");
    
    
    
    
    ?>
    PHP:
    This should be a easy enough script to change layout and add to the index page and how to set up other pages


    Enjoy
    NukeZilla
     
    Xifyhosting, Sep 20, 2011 IP
  2. akumar2

    akumar2 Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    ;) Please paste your complete code if there will be ay suggestion I will let you know ..
     
    akumar2, Sep 20, 2011 IP
  3. Xifyhosting

    Xifyhosting Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Sorry might not of explained it every well. This code actually works
     
    Xifyhosting, Sep 20, 2011 IP
  4. NLZ13

    NLZ13 Well-Known Member

    Messages:
    166
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    113
    #4
    Looks nice. Only I would suggest not displaying empty forms when the user provides bad form data.
    Meaning instead of showing a empty input again, showing it like:
    AIM: <input type="text" name="aim" value="<?php echo $_POST['aim']; ?>" />

    People actually hate forms they have to entirely again because of mistyping something. This is a short form so that doesn't matter so much but it's only a hint
     
    NLZ13, Sep 20, 2011 IP
  5. Xifyhosting

    Xifyhosting Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks. Thats a good idea. i forgot about stuff like that
     
    Xifyhosting, Sep 20, 2011 IP
  6. amherstsowell

    amherstsowell Peon

    Messages:
    261
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    It's a good post for a beginners and professionals too NLZ13 opinion is very nice you missed it.
     
    amherstsowell, Sep 20, 2011 IP