How to set the php session time out, I'm trying like below, but I dont think it works
ini_set("session.gc_maxlifetime", 600);
How to find out whether a php session exists or expired using ajax (javascript)?
Regards
How to set the php session time out, I'm trying like below, but I dont think it works
ini_set("session.gc_maxlifetime", 600);
How to find out whether a php session exists or expired using ajax (javascript)?
Regards
For #1 use session_set_cookie_params()
. To expire after 600 seconds
session_set_cookie_params(600)
(note unlike the regular setcookie
function the session_set_cookie_params
uses seconds you want it to live, it should not be time() + 600
which is a common mistake)
For number 2 just make a small script called through AJAX:
<?php
session_start()
if( empty($_SESSION['active']) ) {
print "Expired"
}
else {
print "Active"
}
?>
On the Javascript side (using JQuery)
$.get('path/to/session_check.php', function(data) {
if( data == "Expired" ) {
alert("Session expired");
} else if (data == "Active" ) {
alert("Session active");
}
});
What Shadow Wizard commented about keeping the session alive every time you do the check is true.
But the solution is quite simple. The trick is to perform the AJAX request at an interval larger than the session life time. So if you establish a session timeout of 15 minutes you can check via AJAX every 16 or more.
In order for the above to work, the session timeout is something that you should implement manually. You can read this usefull answer on how to set the session duration.
Hope this helps you or anyone who is looking for the same!