There is an ajax loaded content into a <div>
in the page I use. All the pages in the project are secured under login. And by the logic, if the user keeps the window idle for 300 seconds or more the PHP will redirect to login page. This works good when the page is refreshed and navigated. But the problem I face is, when I load a <div>
or any element with an AJAX output, sometimes the login page is loaded within the element. This is because the window is idle and the PHP server redirects to login page. I need to redirect the main page to the login page instead of loading the redirected login page via AJAX. Ideas please.
PHP check last active and redirect if > 300 seconds,
if(isset($_SESSION['active_last']))
{
if((time()-$_SESSION['active_last'])>300)
{
$_SESSION['toredirect']=$_SERVER['REQUEST_URI'];
$rd_loc=BASEPATH."login/?error=timeout";
header("location: $rd_loc");
}
$_SESSION['active_last']=time();
}
This is common problem as AJAX request will append the response to the place you want to show . You need to handle it by yourself. Here is my solution , in the case of Session expire and request is AJAX i return the status code 401 and then this is how i handle it at global level. I have put this code in the master page/template.
$(document).ajaxError(function (event, jqxhr, settings, exception) {
if (jqxhr.status === 401) {
alert('Session is expired. Login again');
window.location.href = '../Admin/Signout.php';
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With