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

ChatGPT Free Online Download the ChatGPT App Easily

Have you ever wanted to talk to a friendly, smart robot that helps you with…

3 days ago

Free GPT Chat? DeepSeek AI Does It Better

DeepSeek AI is becoming a friendly and powerful new tool in the world of artificial…

2 weeks ago

Spring Boot MySQL Complete CRUD REST API [ Free Sourecode ]

Do you want to become an expert in Spring Boot CRUD operations? This comprehensive tutorial…

2 weeks ago

CREATE Responsive Navigation Bar with FlexBox CSS!

Modern websites must have a navigation bar that is clear and responsive. FlexBox CSS is…

4 weeks ago

Registration with image upload Java Jdbc(Download Source code)

Introduction In this section, we will guide you step by step in the development of an image upload registration system in Java using MySQL and JDBC. In the application, users register…

2 months ago

Touchable shop Pos system using Java

The Touchable Shop POS (Point of Sale) system is a sophisticated software solution developed using…

3 months ago