Logged out from one tab. User should logged out from other tab
I am using jQuery ajax call to update data. I logged in in two different tabs in firefox.
I logged out from one tab. The second tab is not yet refreshed.
When I tried to update data through ajax call the call executed properly.
How do I catch the use开发者_Python百科r is logged out and display error while executing the ajax call?
Before processing the AJAX call on the server side, you should have a check if the user is logged in.
If no session is found, return a special error in the AJAX call; example for JSON:
{"status":"error","error":"loggedout"}
If the application receives this information, display a "Session has timed out" message to the user and redirect them to the login (or other appropriate) page.
If user at first tab was logged out explicitly (not by closing the tab) you can check is he still logged when handling Ajax request by PHP. Eg:
function login () {
$_SESSION['isLogged'] = true;
}
function logout () {
$_SESSION['isLogged'] = false;
}
function isLogged () {
return (isset ($_SESSION['isLogged']) ? $_SESSION['isLogged'] : false);
}
and in your Ajax handler:
if (isLogged ()) {
... some stuff here
}
else {
... return error message here and ask user to log in
}
精彩评论