Join TalkersCode Now !

Get Latest Tutorials And Links On Web Development
And Get Existing Offers Via Email

Session Timeout Warning With Countdown Using PHP, jQuery And HTML

Tuesday, Jan 24, 2017

Tags:- PHP jQuery Ajax HTML


In this tutorial we will show you how to display session timeout warning with countdown using PHP, jQuery and HTML.When user logged in using login form we display a countdown and after the countdown end user will be logged out automatically.You may also like simple countdown using jQuery CSS and HTML.


See Demo
Sesison Timeout Warning With Countdown Using PHP, jQuery And HTML


To Display Session Timeout Warning With Countdown It Takes Only Three Steps:-

  1. Make a PHP file and define markup and scripting
  2. Make a PHP file to do login in and logout
  3. Make a CSS file and define styling


Step 1.Make a PHP file and define markup and scripting

We make a PHP file and save it with a name session_countdown.php

<?php
session_start();
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="session_style.css">
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function start_countdown()
{
 var counter=10;
 myVar= setInterval(function()
 { 
  if(counter>=0)
  {
   document.getElementById("countdown").innerHTML="You Will Be Logged Out In <br>"+counter+" Sec";
  }
  if(counter==0)
  {
   $.ajax
   ({
     type:'post',
     url:'login_logout.php',
     data:{
      logout:"logout"
     },
     success:function(response) 
     {
      window.location="";
     }
   });
   }
   counter--;
 }, 1000)
}
</script>
</head>
<body>
<div id="wrapper">

<?php
if($_SESSION['password']=="123" && $_SESSION['name']=="123")
{
 ?>
 <script>start_countdown();</script>
 <p id="countdown"></p>
 <?php
}
else
{
 ?>
 <form method="post" action="login_logout.php" id="login_form">
  <h1>LOGIN TO PROCEED</h1>
  <input type="text" name="name" placeholder="Name">
  <input type="password" name="pass" placeholder="*******">
  <input type="submit" name="submit_pass" value="DO SUBMIT">
  <p>"Name : 123" , "Password : 123"</p>
  <p><font style="color:red;"><?php echo $error;?></font></p>
 </form>
 <?php	
}
?>

</div>
</body>
</html>

In this step we first check if user is already logged in or not if user is not logged in we display the login form and if user is logged in we start the countdown.We create start_countdown() function to display the countdown and it counts to 10 and when the countdown ends we clear the session variable using Ajax call and refresh the page to display login form again.You may also like date time countdown timer using JavaScript.



Step 2.Make a PHP file to do login in and logout

We make a PHP file and save it with a name login_logout.php

<?php
session_start();

if(isset($_POST['submit_pass']) && $_POST['name'] && $_POST['pass'])
{
 $name=$_POST['name'];
 $pass=$_POST['pass'];
 if($name=="123" && $pass=="123")
 {
  $_SESSION['name']=$name;
  $_SESSION['password']=$pass;
 }
 else
 {
  $error="Incorrect Pssword";
 }
}
if(isset($_POST['logout']))
{
 unset($_SESSION['name']);
 unset($_SESSION['password']);
 echo "success";
 exit();
}
?>

In this step we create two isset() conditions to do login or logout.In first isset() condition we get user name and password entered by user and match the value with our predefined value if it matches we insert user details in session variables.In second isset() condition we simply unset session variables when countdown ends.You may also like create password protected webpage using PHP, HTML and CSS.



Step 3.Make a CSS file and define styling

We make a CSS file and save it with a name session_style.css

body
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:100%;
 font-family: "Myriad Pro","Helvetica Neue",Helvetica,Arial,Sans-Serif;
 background-color:#BDBDBD;
}
#wrapper
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:995px;
}
#wrapper h1
{
 margin-top:50px;
 font-size:45px;
 color:#424242;
}
#wrapper p
{
 font-size:16px;
}
#wrapper #countdown
{
 color:#424242;
 font-size:25px;
}
#login_form
{
 margin-top:50px;
 background-color:white;
 width:350px;
 margin-left:310px;
 padding:20px;
 box-sizing:border-box;
 box-shadow:0px 0px 10px 0px #424242;
}
#login_form h1
{
 margin:0px;
 font-size:25px;
 color:#424242;
}
#login_form input[type="password"],input[type="text"]
{
 width:250px;
 margin-top:10px;
 height:40px;
 padding-left:10px;
 font-size:16px;
}
#login_form input[type="submit"]
{
 width:250px;
 margin-top:10px;
 height:40px;
 font-size:16px;
 background-color:#424242;
 border:none;
 box-shadow:0px 4px 0px 0px #2E2E2E;
 color:white;
 border-radius:3px;
}
#login_form p
{
 margin:0px;
 margin-top:15px;
 color:#2E2E2E;
 font-size:17px;
 font-weight:bold;
}

You can also view our login form with limited login attempts tutorial and add more security in this code. That's all, this is how to display session timeout warning with countdown using PHP, jQuery and HTML.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email

Are You Starting A New Blog
Do You Want Best Web Hosting?

Try World's Best Web Hosting Provider BlueHost
At Just $3.95/mo

Hurry Up! Limited Time Offer

Yes, I Want To Check Out BlueHost

Check The World's Best SEO And All-In-One Marketing Toolkit
For Digital Marketing Professionals SEMRUSH

Check Out SEMRUSH