0

so i'm writing something similar to a chat app and need a way to detect when a user is no longer active. essentially i need to let OTHER users of the app know that user X logged out if they left the page or closed their browser. Any way for a server-side script to figure this out aside from "i got no requests from user X in 15 mins so i'll assume he's out?"

Michael Drob
  • 350
  • 3
  • 7
  • The logic is here: http://stackoverflow.com/questions/8806876/1-1-chat-system-using-php-mysql/8807233#8807233 – Dipu Raj Jan 12 '12 at 22:14
  • Refer to this post: http://stackoverflow.com/questions/887919/how-to-detect-if-the-user-is-logout-in-php – James Corr Jan 12 '12 at 22:16

1 Answers1

0

Just as they said in the links in the comments you need to use AJAX. The easiest way to do this (in my opinion) is with JQuery, here is an example below:

$(document).ready(function(){
    window.setInterval(function(){
        initiateSessionCheck();
    5000);

    function initiateSessionCheck() {
        $.ajax({
            type : 'POST',
            url : 'sessioncheck.php',
            success : function(data){
                //success
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                //error do something they are no longer active
            }
        });

    }
});

Now in your sessioncheck.php file you'll want to do this

session_start();

if (!$_SESSION) { //you could check a session variable explicitly like $_SESSION['user_id'] != ''
    $return['error'] = true;
} else {
    $return['error'] = false;
}

echo json_encode($return);

It's basic but it should do the trick!

Jeff Wooden
  • 5,339
  • 2
  • 19
  • 24
  • but this seems to check YOUR session status - not the status of other people in the room. Meaning if you and I are talking and I leave, MY session will close, but how will you be aware of that? how is the server aware of a session being closed? The php does not share sessions between me and you – Michael Drob Jan 13 '12 at 03:23
  • wait I think i got it - it's sort of a keepalive .. every time i ping to sessionchec, i'd write to the DB saying "user X pinged at 'timestamp'".. and then the list of active users would be determined by their last ping ... right? – Michael Drob Jan 13 '12 at 03:25
  • Ok well you could use the same function to write a date and time to a specific table every x seconds. Then setup a cron that checks the same table and if x time has elapsed on the date time stamp then you can assume the user is gone and has no active session (they would have to leave the page to kill the ajax calls). If the session is over then you can write to a column in the users table called active_session (for instance) and set it to 0 if they are not active - make sure it is set to 1 when they login. Then in the same ajax call for the other user which also writes the date time stamp, you – Jeff Wooden Jan 13 '12 at 03:40
  • can also query the users table for the user you are chatting with to see if the active_session column is still set to 1... make sense? – Jeff Wooden Jan 13 '12 at 03:41
  • well, as it is, i'm already grabbing active user data (using EventSource) from the db so i'd just remove the inactive user thus only returning the relevant ones – Michael Drob Jan 13 '12 at 04:08
  • that should be easy enough then, when your event runs, during your user data grab just don't grab users where the time stamp hasn't been updated in x seconds... that should do the trick – Jeff Wooden Jan 13 '12 at 04:18