views:

58

answers:

2

I am using jqury 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 user is logged out and display error while executing the ajax call.

A: 

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
}
Piotr Pankowski
+3  A: 

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.

Tobias Baaz