Tuesday, March 10, 2020

Login/Logout Session Management Example

login.php
<!DOCTYPE html>  
 <html lang="en">  
 <head>  
   <meta charset="UTF-8">  
   <meta name="viewport" content="width=device-width, initial-scale=1.0">  
   <title>Document</title>  
 </head>  
 <body>  
 <h2>Login/Logout in session Management</h2>  
   <form action=""method="post">  
   Username: <input type="text" name="un"><br><br>  
   Password: <input type="text" name="pw"><br><br>  
   <input type="submit" name="b1" Value="LOGIN">  
   </form>  
 </body>  
 </html>  
 <?php  
 session_start();  
 include 'connection.php';  
 if(isset($_POST['b1']))  
 {  
 $uname= $_POST['un'];  
 $pwd= $_POST['pw'];  
 $query = "select * from secetable where username='$uname' && password='$pwd'";  
 $data = mysqli_query($conn, $query);  
 $totRec = mysqli_num_rows($data);  
 if($totRec==1)  
 {  
   // echo "Login Ok";  
   $_SESSION['user_name'] = $uname;  
   $_SESSION['last_login'] = time();  
   header('location:home.php');  
 }else  
 {  
   echo "Login Failed";  
 }  
 }  
 ?> 
If we will not enter correct credentials it will echo Login Failed else will let you to home.php.

home.php
Here we implement both Session management  
1. Time-Bound session Management: We create a global session variable $_SESSION['last_login'] for Implementing Time-bound session management. 
2. Event-Bound Session Management: For Even bound session management we create a logout.php where we destroy our session variable. 
<?php  
 session_start();  
 include 'connection.php';  
 $username = $_SESSION['user_name'];  
 if(isset($_SESSION['user_name']))  
 {  
   if(time()-$_SESSION['last_login']>15)  
   {  
     header('location:logout.php');  
   }  
   else  
   {  
     $_SESSION['last_login'] = time();  
   }  
 }  
 else  
 {  
   header('location:login.php');  
 }  
 $query ="select * from secetable where username='$username '";  
 $data = mysqli_query($conn, $query);  
 $result = mysqli_fetch_assoc($data);  
 echo "Welcome ".$result['name']."<br>";  
 echo "<img src='$result[photo]' width='100'>";  
 ?>  
 <?php  
 session_start();  
 include 'connection.php';  
 $username = $_SESSION['user_name'];  
 if(isset($_SESSION['user_name']))  
 {  
   if(time()-$_SESSION['last_login']>15)  
   {  
     header('location:logout.php');  
   }  
   else  
   {  
     $_SESSION['last_login'] = time();  
   }  
 }  
 else  
 {  
   header('location:login.php');  
 }  
 $query ="select * from secetable where username='$username '";  
 $data = mysqli_query($conn, $query);  
 $result = mysqli_fetch_assoc($data);  
 echo "Welcome ".$result['name']."<br>";  
 echo "<img src='$result[photo]' width='100'>";   
  ?>  
  <p align="justify">Lorem ipsum dolor sit amet consectetur adipisicing elit.    
  Alias reiciendis blanditiis quos pariatur odio adipisci praesentium deleniti    
  fugiat esse accusamus. Ratione aperiam hic nobis molestiae obcaecati dolorum    
  laudantium unde impedit provident reprehenderit quas nostrum dolores quisquam    
  ipsam, nulla quod ipsa autem nam eveniet quidem mollitia. Dolorem, dolorum.    
  Pariatur cupiditate molestias reiciendis veniam, consequuntur, mollitia, esse    
  accusamus aperiam eos velit totam quis? Eaque suscipit ipsa ratione modi earum,    
  odit ducimus. Repudiandae ducimus aliquid, aut officiis quis perferendis, eveniet    
  vero, sint maiores reprehenderit accusamus ipsam tenetur. Porro dolore culpa    
  necessitatibus tenetur inventore minus ipsum error amet similique architecto    
  suscipit ullam fuga, aliquid quo voluptatibus voluptates veritatis commodi nobis    
  mollitia deserunt. Eos aut accusantium neque porro optio ab quasi illo expedita    
  sunt, esse quia incidunt minima provident quis consectetur, ipsa sit corporis    
  dolores, illum unde. Error, cumque. Amet vitae sunt suscipit magni a vel saepe    
  odio dolor eaque illo doloremque, sint sequi doloribus, modi ab officiis sit!    
  Soluta tempora voluptatum facere tempore harum. Rem nulla, officiis numquam    
  assumenda id dolorem nobis pariatur in aut reprehenderit autem commodi rerum eaque    
  veritatis repellendus quos distinctio sed aspernatur tempora asperiores, quam corrupti    
  deleniti! Quos labore reprehenderit dolor ullam, sint placeat veniam error officiis in    
  ipsam perspiciatis?    
  </p>    
  <a href="logout.php">Logout</a> 
logout.php
<?php  
 session_start();  
 session_unset();  
 header('location:login.php');  
 ?>