My readers are asking that how do we display message to users that their sessions got expired. So I thought this tutorial will really useful to lots of web developer who working on session based websites. We should always notify our users that their PHP session timeout via popup or alert box. In this example, we are going to show timeout alert in a popup
I have used javascript timing event – setInterval() – executes a function, over and over again, at specified time intervals
The setInterval() method will wait a specified number of milliseconds, and then execute a specified function, and it will continue to execute the function, once at every given time-interval.
Tutorials you may also like
- Laravel vs CakePHP – Which One is Best for Web App Development in 2020?
- Python vs PHP for Web Applications in 2020
- How to login with username or email in PHP & MYSQL
- How to find the difference between two dates in php?
- How toremove index.php from Codeigniter URL ?
jQuery Code – executes every 5 seconds
var check_session;
function CheckForSession() {
var str="chksession=true";
jQuery.ajax({
type: "POST",
url: "chk_session.php",
data: str,
cache: false,
success: function(res){
if(res == "1") {
alert('Your session has been expired!');
}
}
});
}
check_session = setInterval(CheckForSession, 5000);
In the above javascript function called CheckForSession(), it will called every 5 seconds to check the sessions via chk_session.php
PHP code to check session timeout – chk_session.php
session_start();
$name = $_SESSION["w3name"];
if($name == '')
{
//session expired
echo "1";
} else {
//session not expired
echo "0";
}
If sessions are empty then the response will be “1”.
I hope you will like this tutorial. You can use this code in your live projects.
Please don’t forget to share and subscribe to latest updates of the blog. Comments and feedbacks are always welcome!
Thanks!
I have downloaded the script but it is not working localy even when my internet is on
Hi kressly,
Could you please share the teamviewr details to my email I’d itzurkarthi@gmail.com . so that I will help you out immediately..
Thanks
Yes, i just did (share the teamviewers details by sending it to your email)
Great Solution!
Hi, This is working fine in my site. But it’s using my site is very slow.
How to solve this problem?
Thanks,
Sundar
Apparently here nobody answers nobody. I have also asked a lot of question but no one gave an answer and that is not good at all. Sorry Bro
I think that because ajax sent 5s data once, it will cost a lot of server resources
Arem’t you risking a nasty error message if the session has indeed expired and the noted session key no longer exists? Shouldn’t ‘array_key_exists()’ be used here?
but in this way the session is kept alive by the ajax call, it never expires!!
Very true! I have been thinking this over myself. As soon as you make a server-side call (ajax) the session would refresh automatically. This is a ‘keep alive’ method and not ‘session expiry detection’. I need a method to detect session expiry.
you can prevent the alert box only one time rather to each 5 sec. and the best way is to use local-storage rather then ajax , in this you are always contacting to the server this is not a good.