Simple Login Snippet

Page 1 of 1

0 Replies - 330 Views - Last Post: 28 December 2006 - 12:19 PM

#1 AdamG  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 95
  • Joined: 12-January 05

Simple Login Snippet

Posted 28 December 2006 - 12:19 PM

Description: I commented most of the script.

You'll need to setup a DB of some sort, I use MySQL here, to store user information (e.g. username/email, password, etc.). Then make the needed changes to this script.

If you have any questions shoot me a PM. EnjoyI developed this a couple years ago for systems that require user authentication. I use a slightly different variant now, but figured some may like to use this one.
     //once the sign-in form is submitted, run the following
     if (isset($_POST['Submit'])){

          // Check for user existence in DB
          $checkUser = "SELECT column_id, column_name, column_pass FROM table1 WHERE column_name = '" . $_POST['username'] . "'";
          // The die() function aids in troubleshooting SQL query problems IF you include the mysql_error() function
          $checkResult = mysql_query ($checkUser) or die(mysql_error());     // the die() function aids in troubleshooting SQL query problems     
          // Here we check to make sure the username exists in the DB
          if(mysql_num_rows($checkResult) > 0){

               // Retreive the SQL results for further processing
               $user = mysql_fetch_array($checkResult);
               // Set a variable for the password the user entered at login.
               $Pass = $_POST['password'];
               $UserID = $user['column_id'];

               // Check the password the user entered against the password stored in the DB
               if ($Pass == $user['column_pass']){
                    
                    header("Location: member.php");

               }
               else {

                    // The password did not authenticate, so send them back to the login page and display error
                    header("Location: index.php?err=pass");

               }

          } else {
               
               // The user entered a name that did not exist in the DB, send back to login page and display error
               header("Location: index.php?err=user");

          }
     }



Is This A Good Question/Topic? 0
  • +

Page 1 of 1