Create simple login page with PHP and MySQL

With login page allowing the users to enter in the website and manage its content. It important to initialize the $_SESSION when the user successfully logged in.

This helps to detect the user is actual logged in or not when going to another web pages and also display content according to it.

Create simple login page with PHP and MySQL


  1. Table structure
  2. Configuration
  3. HTML
  4. CSS
  5. PHP
  6. Conclusion



1. Table structure

I am using users table in the tutorial example.

CREATE TABLE `users` (
  `username` varchar(80) NOT NULL,
  `password` varchar(80) NOT NULL

It has following data –

id username password
1 yssyogesh 12345
2 sona 123456
3 vishal 32145
4 vijay 147526


2. Configuration

Create a new config.php file.

Completed Code

$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "tutorial"; /* Database name */

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
 die("Connection failed: " . mysqli_connect_error());


Create a login page layout which has two input type elements for entering username and password and a button to submit the form.

Completed Code

<div class="container">
    <form method="post" action="">
        <div id="div_login">
                <input type="text" class="textbox" id="txt_uname" name="txt_uname" placeholder="Username" />
                <input type="password" class="textbox" id="txt_uname" name="txt_pwd" placeholder="Password"/>
                <input type="submit" value="Submit" name="but_submit" id="but_submit" />

4. CSS

/* Container */
    margin:0 auto;

/* Login */
    border: 1px solid gray;
    border-radius: 3px;
    width: 470px;
    height: 270px;
    box-shadow: 0px 2px 2px 0px  gray;
    margin: 0 auto;

#div_login h1{
    margin-top: 0px;
    font-weight: normal;
    padding: 10px;
    background-color: cornflowerblue;
    color: white;
    font-family: sans-serif;

#div_login div{
    clear: both;
    margin-top: 10px;
    padding: 5px;

#div_login .textbox{
    width: 96%;
    padding: 7px;

#div_login input[type=submit]{
    padding: 7px;
    width: 100px;
    background-color: lightseagreen;
    border: 0px;
    color: white;

5. PHP

On submit, button click checks the user has entered the username and password or not. If it has entered then check username and password in users table.

When the query return value is greater than 0 then initialize $_SESSION['uname'] variable and redirect the user to home.php using the header.

Complete Code

include "config.php";


    $uname = mysqli_real_escape_string($con,$_POST['txt_uname']);
    $password = mysqli_real_escape_string($con,$_POST['txt_pwd']);

    if ($uname != "" && $password != ""){

        $sql_query = "select count(*) as cntUser from users where username='".$uname."' and password='".$password."'";
        $result = mysqli_query($con,$sql_query);
        $row = mysqli_fetch_array($result);

        $count = $row['cntUser'];

        if($count > 0){
            $_SESSION['uname'] = $uname;
            header('Location: home.php');
            echo "Invalid username and password";




Create new home.php file.

First, check whether the user is logged or not by checking the $_SESSION['uname'] variable. If its not set then redirect the user to index.php for login.

Created a logout button which destroy the $_SESSION['uname'] variable and redirect to index.php web page on click.

Complete Code

include "config.php";

// Check user login or not
    header('Location: index.php');

// logout
    header('Location: index.php');
<!doctype html>
        <form method='post' action="">
            <input type="submit" value="Logout" name="but_logout">

6. Conclusion

In this tutorial, I showed how you can create a simple login page with PHP and MySQL.

You must have to initialize the $_SESSION variable to detect the user when traversing to other pages and destroy it when the user clicks the logout buttons.

Related Post

Spread the love

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *