How to Create Login Page in PHP and MySQL with Session

How to Create Login Page in PHP and MySQL with Session In my previous tutorial called "How to Create Secure Login Page in PHP/MySQL", I explain on the three ways on how create a login page using PHP/MySQL. This time, I will add more functionality on this tutorial. We will add session so that we will know if the visitor has logged in on our site or not. In PHP, there is a variable called $_SESSION. Session allow you to store information and used it in any pages that you like. An example of this is storing of login information. If the visitor has logged in into your site, you can save the id and username of that particular visitor so you will not what privilege you can give to him/her. Unlike cookie, session is stored on the server. This means that session is more secure compare with cookie. Now let's begin creating our script. Create an html file called "login.html" and put the following code:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>Login Form</title>
  5. </head>
  6.  
  7. <form id="form1" name="form1" method="post" action="login.php">
  8.         <table width="510" border="0" align="center">
  9.                 <tr>
  10.                         <td colspan="2">Login Form</td>
  11.                 </tr>
  12.                 <tr>
  13.                         <td>Username:</td>
  14.                         <td><input type="text" name="username" id="username" /></td>
  15.                 </tr>
  16.                 <tr>
  17.                         <td>Password</td>
  18.                         <td><input type="password" name="password" id="password" /></td>
  19.                 </tr>
  20.                 <tr>
  21.                         <td>&nbsp;</td>
  22.                         <td><input type="submit" name="button" id="button" value="Submit" /></td>
  23.                 </tr>
  24.         </table>
  25. </form>
  26. </body>
  27. </html>
Next, create a PHP file called "login.php" and put the following code:
  1. <?php
  2.  
  3. $username = $_POST['username'];
  4. $password = $_POST['password'];
  5.  
  6. $conn = mysql_connect('localhost', 'root', '');
  7. mysql_select_db('login', $conn);
  8.  
  9. $username = mysql_real_escape_string($username);
  10. $query = "SELECT id, username, password, salt
  11.        FROM member
  12.        WHERE username = '$username';";
  13.                
  14. $result = mysql_query($query);
  15.  
  16. if(mysql_num_rows($result) == 0) // User not found. So, redirect to login_form again.
  17. {
  18.     header('Location: login.html');
  19. }
  20.  
  21. $userData = mysql_fetch_array($result, MYSQL_ASSOC);
  22. $hash = hash('sha256', $userData['salt'] . hash('sha256', $password) );
  23.  
  24. if($hash != $userData['password']) // Incorrect password. So, redirect to login_form again.
  25. {
  26.     header('Location: login.html');
  27. }else{ // Redirect to home page after successful login.
  28.         session_regenerate_id();
  29.         $_SESSION['sess_user_id'] = $userData['id'];
  30.         $_SESSION['sess_username'] = $userData['username'];
  31.         session_write_close();
  32.         header('Location: home.php');
  33. }
  34. ?>
If you notice, we have added two line at the very top of the page. The lines are: It is very important to declare these two lines above of any code. Adding it below will not simply work. Take note also that we change the SQL statement on our previous tutorial. This is the line that has change:
  1. $query = "SELECT id, username, password, salt
  2.        FROM member
  3.        WHERE username = '$username';";
And we added the following line after successful login:
  1.         session_regenerate_id();
  2.         $_SESSION['sess_user_id'] = $userData['id'];
  3.         $_SESSION['sess_username'] = $userData['username'];
  4.         session_write_close();
Next, create another PHP file called "home.php" and put the following code:
  1. <?php
  2. //Start session
  3.  
  4. //Check whether the session variable SESS_MEMBER_ID is present or not
  5. if(!isset($_SESSION['sess_user_id']) || (trim($_SESSION['sess_user_id']) == '')) {
  6.         header("location: login.html");
  7.         exit();
  8. }
  9. ?>
  10. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  11. <html xmlns="http://www.w3.org/1999/xhtml">
  12. <head>
  13. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  14. <title>Home Page</title>
  15. </head>
  16.  
  17. <body>
  18. <h1>Welcome, <?php echo $_SESSION["sess_username"] ?></h1>
  19. </body>
  20. </html>
We have also added session_start() function at the very top and check if the variable sess_user_id was set or not. If the variable is not set, then we will redirect the page url to the login page.

Add new comment