php

Login and Logout Form with Session in PHP MySQL

This Tutorial will teach you how to make the Login and Logout Form with Session in PHP MySQL. In order to create the project i have used editor as PHPStrom

First Step you have to Establish the Database Connection. i have created the file which name is db.php.

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pmlogin";

$conn = mysqli_connect($servername,$username,$password,$dbname);


?>

After that you have to create the Login Form. and call the database connection page name like this way include “db.php”;

<?php

include "db.php";

session_start();

if($_SERVER["REQUEST_METHOD"]=='POST')
{
    $username = mysqli_real_escape_string($conn,$_POST['username']);
    $password = mysqli_real_escape_string($conn,$_POST['password']);


    $sql = "select * from user where user = '$username' and pass = '$password'";


    $result = mysqli_query($conn,$sql);

    if(mysqli_num_rows($result)== 1)
    {
        while ($row = mysqli_fetch_assoc($result))
        {
            $user_id = $row['id'];
            $user_name = $row['user'];
            $_SESSION["id"]=$id;
            $_SESSION["utype"]=$type;
            $_SESSION['user'] = $user_name;
        }

        header('location:index.php');
    }

    else
    {
        echo "<script> alert('Username or Password do not Match') </script>";
    }

}

?>



<html xmlns="http://www.w3.org/1999/xhtml"><head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title><?php echo ADM_TITLE; ?></title>
    <style type="text/css">
        <!--
        body,td,th {
            color: #000000;
        }
        body {
            background-color: #F0F0F0;
        }
        .style1 {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            padding: 12px;
            text-decoration: none;

            line-height: 25px;
            border-radius: 4px;

        }
        .style2 {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 16px;
            padding: 12px;
            text-decoration: none;
            font-size: 18px;
            line-height: 25px;
            border-radius: 4px;

        }




    </style>

    <script language="javascript">
        function MM_preloadImages() { //v3.0
            var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
                var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
                    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
        }
    </script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

</head>

<body onload="MM_preloadImages('pagination/ajax-loader.gif');">


<div class="container">

<table width="100%" height="100%" border="0"  cellspacing="0" width="100%" align="center">
    <tr>
        <td align="center" valign="middle"><?php //echo $sess_id; ?>
            <table class="table-bordered" width="350" border="0" cellpadding="3" cellspacing="3" bgcolor="#FFFFFF" >
                <form name="frm_login" id="frm_login" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                    <tr>
                        <td height="25" colspan="2" align="left" valign="middle" bgcolor="#FF9900" class="style2">
                            <div align="center">
                                <strong>Administrator Panel</strong>
                            </div>
                        </td>
                    </tr>

                    <tr>
                        <td width="118" align="left" valign="middle" class="style1">User Name</td>


                        <td width="225" align="left" valign="middle"><input type="text" class="form-control" size="10px" id="username" placeholder="Enter email" name="username"></td>
                    </tr>

                    <tr>

                        <td align="left" valign="middle"><input type="password" class="form-control" size="10px"  id="password" placeholder="Enter password" name="password"></td>
                    </tr>

                    <tr>
                        <td align="left" valign="middle" class="style1">Role</td>
                        <td align="left" valign="middle" class="style1">


                                <select class="form-control" id="project_status" name="project_status"
                                        placeholder="Project Status" required>
                                    <option value="">Please Select</option>
                                    <option value="1">On Going</option>
                                    <option value="2">On Hold</option>
                                    <option value="3">Completed</option>
                                    <option value="4">Canceled</option>
                                </select>


                        </td>

                    </tr>

                    <tr>

                        <td align="right" colspan="2"    valign="middle">
                            <input type="submit" name="button" class="btn btn-info" id="button" value="Login" />
                            <input type="submit" name="button" class="btn btn-warning" id="button" value="Reset" />
                        </td>


                    </tr>

                </form>
            </table>
        </td>
    </tr>
</table>
    </div>
</body>
</html>

Login we made a user validation. if the user enter the correct username along with password it will redirect  to the index.php page along with session.other wise it will show the  Error message as Username or Password do not Match.

if you want logout the page click logout link.

Logout.php

<?php

session_start();

if(session_destroy())
{
    header("Location: login.php");
    exit();
}

?>

i have attached the video link below. which will do this tutorials step by step.

 

admin

Recent Posts

Employee Working Hours Calculation System using C#.net

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…

1 week ago

Java Payroll System Calculate Employee Overtime

Act as a Java developer to create a program that calculates the gross wage for…

2 weeks ago

Employee Working Hours Calculation System using Java

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…

2 weeks ago

Laravel 11 School Management System

In this tutorial, we will teach you how to create a simple school management system…

3 weeks ago

How to Make Admin Panel Using React MUI

I have design the Admin Basic templete using React MUI Design Admin Dashboard and Login.Here…

1 month ago

Install Laravel Breeze for Authentication Using Laravel 11

In this tutorial ,i am to going teach the Laravel Breeze.Laravel Breeze provides a simple…

1 month ago