PHP and MySQLi Login Page

« Previous Tutorial Next Tutorial »

Here you will learn about how to create login page with validation using PHP and MySQLi.

What to do Before Creating Login Page

Before creating login page using PHP and MySQLi, be sure that you have already learned previous tutorial, and inserted some record into the table using SignUp page tutorial, specially one record with username and password as codescracker that will be used in this tutorial for login checkup.

PHP MySQLi Login Page Example

Here is an example of login page using PHP and MySQLi.

   // you have already learned about session
   $conn = mysqli_connect('localhost', 'root', '', 'codescracker');
      echo "<p>Error in connection to database.</p>";
      // checks whether logout button is clicked or not
         // below code is used to destroy all the session
         // below code is used to redirect users to codescracker.php page
         header("location: codescracker.php");
         // below code is used to skip executing the remaining code
         // after this
      $username = $password = "";
      $username = $_POST["username"];
      $username = filter_login_input($username);
      $password = $_POST["password"];
      $password = filter_login_input($password);
      $qry = "select * from users where username='$username' and password='$password'";
      $res = $conn->query($qry);
         $_SESSION['login'] = $username;
         $loginCheck = "No";
   function filter_login_input($loginData)
      $loginData = trim($loginData);
      $loginData = stripslashes($loginData);
      $loginData = htmlspecialchars($loginData);
      return $loginData;
   <title>PHP and MySQLi Login Page Example</title>
   function checkBeforeLogin()
         alert("Enter Username");
         return false;
         alert("Enter Your Password");
         return false;
      return true;

      echo "<p>You are successfully logged in.</p>";
      echo "<p>Now you can access the admin page.</p>";
      echo "<form method=\"post\">";
      echo "<input type=\"submit\" name=\"logout\" value=\"LogOut\">";
      echo "</form>";
   <form name="loginForm" method="post" onSubmit="return checkBeforeLogin()">
   <input name="username" type="text" placeholder="enter username" maxlength="40" required><br/>
   <input name="password" type="password" placeholder="enter password" maxlength="40" required><br/>
         echo "Invalid data<br/>";
   <input type="submit" name="login" value="LogIn">

Save the above code in a file with name codescracker.php inside the directory C:\xampp\htdocs\ and run the above example code of login page just by opening any web browser and typing localhost/codescracker.php.

Here is the sample output produced by the above example of login page using PHP and MySQLi.

php mysqli login page

Now, let's first enter wrong data as shown in the following figure:

create login page php mysqli

After entering the above wrong data (username and password that is not stored inside the database), click on the LogIn button, you will see the following output:

php mysqli login page code

Now enter the right data this time, that is, codescracker as username and password both and click on the LogIn button. As username and password as codescracker is stored inside the database, therefore here is the output that will be produce in your browser:

php mysqli login page example

Now again type localhost/codescracker.php in your web browser, then the output will be same, that is, you are logged in, as we have used session here to keep the login information of the user.

If you want to logout from the page, then you must have to click on the LogOut button.

PHP Online Test

« Previous Tutorial Next Tutorial »

© Copyright 2021. All Rights Reserved.