Login Logout system in PHP with SESSION | PHP and MYSQL Database

 


Login with session using PHP and MYSQL database


What is session?

The session is used to access the various pages of an entire website. A session creates a temporary directory on the server. With the help of session variables and their value is stored.

A session is used to store the information on the server rather than the client computer.

Create a MYSQL database table using Query

Whenever we create a login system we need an MYSQL database. We can login by fetching user data from the MYSQL database table. In this tutorial, we create a simple login form using PHP and MYSQL database. Let's create a database table using the MYSQL query.

 CREATE TABLE `login` (  
  `id` int(11) NOT NULL,  
  `username` varchar(255) NOT NULL,  
  `password` varchar(255) NOT NULL  
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;  

Insert data into MYSQL database table for Login with session

To insert the data into the table, you can use the registration form or insert query. When you create a login form, you have to fetch the data. Unless you have data in the database, you will not be able to create the login system. You can use the registration form to insert data or you can perform insert query to insert data into the database table. Let's insert the data into database table using MYSQL query.

 INSERT INTO `login` (`id`, `username`, `password`) VALUES  
 (1, 'admin', 'admin');  

Database Connect MYSQL and PHP

 $conn=mysqli_connect("localhost","root","","tutorials");  

We are used mysqli_connect() function for a new connection. We are passed this information as a parameter and stored in a variable $conn. You can change database host, database name, database username, and database Password according to your needs.

Login with session pages structure in PHP 

In this tutorials, we use the session to access session data on pages of the website. We create pages to access session data. 

After the destroyed session, unknown persons cannot access the pages. This is the main advantage of PHP session. 

Let's create pages 

1. index.php 

After session created the user redirects to main page(index.php) 

2. login.php 

The login page handles the user input information. This is the starting page of login system. Without login success, user cannot access pages. 

3. logout.php 

We use unset() function to the destroyed complete session. This is used to logout. After logout, the user cannot access another pages. 

When a admin wants to visit index.php without login the session returns to the login page. The user can not directly access the index.html page without login. 

Let's implement a complete Login system using PHP and MYSQL database. 

Make a login form for user login using PHP session 

login.php
 <?php   
 session_start();  
 $conn=mysqli_connect("localhost","root","","tutorials");  
 $msg="";  
 if (isset($_POST['submit'])) {  
      //echo "<pre>";  
      //print_r($_POST);  
      $email=mysqli_real_escape_string($conn,$_POST['email']);  
      $password=mysqli_real_escape_string($conn,$_POST['password']);  
      $sql=mysqli_query($conn,"select * from login where username='$email' && password='$password'");  
      $num=mysqli_num_rows($sql);  
      if ($num>0) {  
           //echo "found";  
           $row=mysqli_fetch_assoc($sql);  
           $_SESSION['USER_ID']=$row['id'];  
           $_SESSION['USER_NAME']=$row['username'];  
           header("location:index.php");  
      }else{  
           $msg="Please Enter Valid details !";  
      }  
 }  
 ?>  
 <!DOCTYPE html>  
 <html>  
 <head>  
      <meta charset="utf-8">  
      <meta name="viewport" content="width=device-width, initial-scale=1">  
      <link rel="stylesheet" type="text/css" href="css/style.css">  
      <title>Login Page</title>  
 </head>  
 <body>  
 <div class="main">  
      <div class="flex">  
           <div class="content">  
                <h2 class="title">Login</h2>  
                <form method="post" action="">  
                     <label for="username">Username</label>  
                     <div class="box">  
                          <input type="text" name="email" placeholder="Username" class="form-control" required>  
                     </div>  
                     <label for="password">Password</label>  
                     <div class="box">  
                          <input type="password" name="password" placeholder="Password" class="form-control" required>  
                     </div>  
                     <div class="btn-box">  
                          <input type="submit" name="submit" value="Login" class="btn submit-btn">  
                     </div>  
                     <div class="error">  
                          <?php echo $msg ?>  
                     </div>  
                </form>  
           </div>  
      </div>  
 </div>  
 </body>  
 </html>  

PHP code for login using session

 <?php   
 session_start();  
 $conn=mysqli_connect("localhost","root","","tutorials");  
 $msg="";  
 if (isset($_POST['submit'])) {  
      //echo "<pre>";  
      //print_r($_POST);  
      $email=mysqli_real_escape_string($conn,$_POST['email']);  
      $password=mysqli_real_escape_string($conn,$_POST['password']);  
      $sql=mysqli_query($conn,"select * from login where username='$email' && password='$password'");  
      $num=mysqli_num_rows($sql);  
      if ($num>0) {  
           //echo "found";  
           $row=mysqli_fetch_assoc($sql);  
           $_SESSION['USER_ID']=$row['id'];  
           $_SESSION['USER_NAME']=$row['username'];  
           header("location:index.php");  
      }else{  
           $msg="Please Enter Valid details !";  
      }  
 }  
 ?>  

Create the main page after login success Now, create the main page index.html page. It is the index page of the login system. First of all, we start the session by session_start() function at the beginning of the page. After that, we check if the session is not created than return to the login page (login.php ).

index.php

 <?php   
 session_start();  
 $conn=mysqli_connect("localhost","root","","tutorials");  
 if (!isset($_SESSION['USER_ID'])) {  
      header("location:login.php");  
      die();  
 }  
 ?>  
 Login with session using PHP and MYSQL database  
 <!DOCTYPE html>  
 <html>  
 <head>  
      <meta charset="utf-8">  
      <meta name="viewport" content="width=device-width, initial-scale=1">  
      <title>Dashboard</title>  
      <style>  
           @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;700&display=swap');  
           body{  
                display: flex;  
                justify-content: space-around;  
                font-family: 'Poppins', sans-serif;  
           }  
      </style>  
 </head>  
 <body>  
 <h1>Welcome <?php echo $_SESSION['USER_NAME'] ?></h1><br>  
 <h2><a href="logout.php">Logout</a></h2>  
 </body>  
 </html>  

Users can access all pages by the login. If the session is not created or destroyed then redirect to the login.php page.

You can also read 

Post a Comment

0 Comments