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

login.php

 <?php  
 //login.php  
 session_start();  
 if(isset($_POST["sub"]))  
 {  
      $_SESSION["name"] = $_POST["name"];  
      header("location:index.php");       
 }  
 ?>  
 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Webslesson Tutorial</title>  
           <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>  
           <script src="jquery.js"></script>  
           <style>  
                #box  
                {  
                     width:600px;  
                     background:gray;  
                     color:white;  
                     margin:0 auto;  
                     padding:10px;  
                     text-align:center;  
                }  
           </style>  
      </head>  
      <body>  
           <div id="box">  
                <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');  
 ?>  

index.php

 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Webslesson Tutorial</title>  
           <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>  
           <script src="jquery.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'>".$_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>  

check_session.php

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

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. It doesn't work for me. I have jquery 1.x version. I get no alert prompt. I just copied and pasted the code.

    ReplyDelete