Thursday, 31 March 2016

Use Ajax with Jquery in PHP to check Session Expired



In this post we are going to learn how to use Ajax with Jquery to check expired php session. I teach this tutorial will absolutely effective to lots of website developer who work on session based websites. When suppose user open website in more than one tab and suppose user logout from one tab and on other tab suppose user open website page then at that time this functionality used. If we used ajax with jquery to check session has expired or not. If session has expired then alert message your session has expired will appear on that tab where website open. I have used setInterval() method for execute function on regular time interval. I have make one function and in this function I have used ajax function call with the help of ajax function it will check session has expired or not.



Source Code


index.php


<!DOCTYPE html>  
 <html>  
      <head>  
           <title>Webslesson Tutorial</title> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
           <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
           <style>  
                #box  
                {  
                     width:600px;  
                     background:gray;  
                     color:white;  
                     margin:0 auto;  
                     padding:10px;  
                     text-align:center;  
                }  
           </style>  
      </head>  
      <body>  
      <?php  
      session_start();  
      if(isset($_SESSION["name"]))  
      {  
          echo "<h1 align='center'>Use Ajax with Jquery in PHP to check Session Expired</h1>";
           echo "<h1 align='center'>".$_SESSION["name"]."</h1>";  
           echo "<p align='center'><a href='logout.php'>Logout</a></p>";  
      }  
      else  
      {  
           header('location:login.php');  
      }  
      ?>  
      </body>  
 </html>  
 <!--index.php !-->  
 <script>  
 $(document).ready(function(){  
       function check_session()
       {
          $.ajax({
            url:"check_session.php",
            method:"POST",
            success:function(data)
            {
              if(data == '1')
              {
                alert('Your session has been expired!');  
                window.location.href="login.php";
              }
            }
          })
       }
        setInterval(function(){
          check_session();
        }, 10000);  //10000 means 10 seconds
 });  
 </script>

login.php


<?php  
 //login.php  
 $connect = mysqli_connect("localhost", "root", "", "testing");
 session_start();  
 if(isset($_POST["sub"]))  
 {  
   $username = mysqli_real_escape_string($connect, $_POST["name"]);
   $password = mysqli_real_escape_string($connect, $_POST["pass"]);
   $query = "SELECT * FROM users WHERE username = '".$username."' AND password = '".$password."'";
   $result = mysqli_query($connect, $query);
   if(mysqli_num_rows($result) > 0)
   {
    $_SESSION["name"] = $_POST["name"];
    header("location:index.php"); 
   }
      else
   {
    echo '<script>alert("Wrong Data")</script>';
   }    
 }  
 ?>  
 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Webslesson Tutorial</title> 
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
           <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
           <style>  
                #box  
                {  
                     width:600px;  
                     background:gray;  
                     color:white;  
                     margin:0 auto;  
                     padding:10px;  
                     text-align:center;  
                     margin-top:100px;
                }  
           </style>  
      </head>  
      <body>  
           <div id="box">
                <h1 align="center">Use Ajax with Jquery in PHP to check Session Expired</h1>  
                <h2>Login</h2>  
                <form method="post">  
                     <input type="text" name="name" id="name" placeholder="Enter Username" class="form-control" /><br />  
                     <input type="password" name="pass" id="pass" placeholder="Enter Pass" class="form-control" /><br />  
                     <input type="submit" name="sub" id="sub" class="btn btn-info" value="Submit" />  
                </form>  
                <br /><br />  
           </div>  
      </body>  
 </html>

logout.php


<?php  
 //logout.php  
 session_start();  
 session_destroy();  
 header('location:login.php');  
 ?>

check_session.php


<?php  
 //check_session.php  
 session_start();
 if(isset($_SESSION["name"]))  
 {  
      echo '0';     //session not expired       
 }  
 else  
 {  
      echo '1';     //session expired  
 }
 ?>

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete