How To Create Login Page In PHP/MySQL

Related Code: How To Create Registration Page In PHP/MySQL In the previous tutorial, we create on How To Create Registration Page In PHP/MySQL. For the continuation of Registration Page Tutorial it should have a Login Page, that's why I create this tutorial on How To Create Login Page In PHP/MySQL. This program works by the user to enter their username or email and password to open the new page. This login page also has a log out function, where the user signs out their account after clicking the logout button on the home page. After the user clicks the logout button, the user cannot go back to the previous page. If the user wants to go back to the previous page, the user must log in again to go back on the previous page. So, let's do this. First, we are going to make our database.

Creating our Table

We are going to make our database. To create a database:
  1. Open the PHPMyAdmin
  2. Create a database and name it as "user_login".
  3. After creating a database name, click the SQL and kindly copy the code below.
  1. --
  2. -- Table structure for table `user`
  3. --
  4.  
  5. CREATE TABLE IF NOT EXISTS `user` (
  6. `user_id` int(100) NOT NULL AUTO_INCREMENT,
  7. `firstname` varchar(100) NOT NULL,
  8. `lastname` varchar(100) NOT NULL,
  9. `username` varchar(100) NOT NULL,
  10. `username2` varchar(100) NOT NULL,
  11. `birthday` varchar(100) NOT NULL,
  12. `gender` varchar(100) NOT NULL,
  13. `number` varchar(100) NOT NULL,
  14. `email` varchar(100) NOT NULL,
  15. `email2` varchar(100) NOT NULL,
  16. `password` varchar(100) NOT NULL,
  17. `password2` varchar(100) NOT NULL,
  18. `profile_picture` varchar(100) NOT NULL,
  19. `cover_picture` varchar(100) NOT NULL,
  20. PRIMARY KEY (`user_id`)
Second, we are going to make our form field.

Creating Form Field

This form field that the user types their username or email and password to log in.
  1. <form method="post" action="signin_form.php" enctype="multipart/form-data">
  2. <h1>Welcome to Biobook</h1>
  3. <h2>Log in</h2>
  4. <tr>
  5. <td><label>Email</label></td>
  6. <td><input type="email" name="email" placeholder="[email protected]" class="form-1" title="Enter your email" required /></td>
  7. </tr>
  8. <tr>
  9. <td><label>Password</label></td>
  10. <td><input type="password" name="password" placeholder="~~~~~~~~~~" class="form-1" title="Enter your password" required /></td>
  11. </tr>
  12. <tr>
  13. <td></td>
  14. <td></td>
  15. </tr>
  16. <tr>
  17. <td></td>
  18. <td></td>
  19. </tr>
  20. <tr>
  21. <td></td>
  22. <td></td>
  23. </tr>
  24. <tr>
  25. <td colspan="2">
  26. <input type="submit" name="submit" value="Log in" class="btn-sign-in" title="Log in" />
  27. <input type="reset" name="cancel" value="Cancel" class="btn-sign-up" title="Cancel" />
  28. </td>
  29. </tr>
  30. </table>
  31. </form>
Third, we are going to make our database connection.

Database Connection

This PHP Script is our database. Copy and paste this then save it as "database.php".
  1. <?php
  2. mysql_select_db('user_login',mysql_connect('localhost','root',''))or die(mysql_error());
  3. ?>
Fourth, we are going to make our saving PHP Script.

Login Script - PHP

  1.  
  2. <?php
  3. include('includes/database.php');
  4.  
  5. if(isset($_POST['submit']))
  6. {
  7. $email=$_POST['email'];
  8. $password=$_POST['password'];
  9. {
  10. $result = mysql_query("SELECT * FROM user WHERE email = '$email' and password='$password'")
  11.  
  12. $row = mysql_fetch_array($result);
  13. $count = mysql_num_rows($result);
  14. if ($count == 0)
  15. {
  16. echo "<script>alert('Please check your username and password!'); window.location='signin.php'</script>";
  17. }
  18. else if ($count > 0)
  19. {
  20. $_SESSION['id'] = $row['user_id'];
  21. header("location:home.php");
  22. }
  23. }
  24. }
  25. ?>
Fifth, we are going to make our Home Page.

Creating Home Page

  1. <!DOCTYPE html>
  2.  
  3. <head>
  4. <title>Welcome To Biobook - Sign up, Log in, Post </title>
  5. <link rel="stylesheet" type="text/css" href="css/home.css">
  6. </head>
  7.  
  8. <?php include ('session.php');?>
  9.  
  10. <div id="header">
  11. <div class="head-view">
  12. <ul>
  13. <li><a href="home.php" title="Biobook"><b>biobook</b></a></li>
  14. <li></li>
  15. <li></li>
  16. <li></li>
  17. <li></li>
  18. <li></li>
  19. <li></li>
  20. <li><a href="timeline.php" title="<?php echo $username ?>"><label><?php echo $username ?></label></a></li>
  21. <li><a href="home.php" title="Home"><label class="active">Home</label></a></li>
  22. <li><a href="profile.php" title="Home"><label>Profile</label></a></li>
  23. <li><a href="photos.php" title="Settings"><label>Photos</label></a></li>
  24. <li><a href="logout.php" title="Log out"><button class="btn-sign-in" value="Log out">Log out</button></a></li>
  25. </ul>
  26. </div>
  27. </div>
  28.  
  29. <h1>
  30. This is your Home Page
  31. </h1>
  32.  
  33. </body>
  34.  
  35. </html>
Output of our Home Page Result - Home Page So, this is it, just follow the steps to have this Login Page or you can download the full source code below by clicking the "Download Code" button below. Related Code: How To Create Registration Page In PHP/MySQL Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment