3

Possible Duplicate:
How can I destroy sessions if user closes the browser window or navigates away from page in php?

Basically I want to set my users to "Offline" by updating my MySQL DB and possibly end their session when they close their browser or the page.

I have seen a website that does this.

Can anyone explain how its done and post an example please?

Thank you in advanced.

Community
  • 1
  • 1
SDZ
  • 726
  • 2
  • 8
  • 21

3 Answers3

3

The only way you can do something when the page is closed or navigated away from is to attach an event handler to the unload event, as Rocket suggests. However, you shouldn't rely on this event to trigger, as a lot of things may prevent it. The browser may stop it on order to focus it's resources on other tasks, the user may loose it's connection, the browser may be terminated, etc.

The most reliable way to keep track of users and session is to have them send keepAlive messages to the server at a given interval. Then you'll know that the user left sometime between a given timestamp and the interval of the keepAlive message.

On the server, you can then traverse the sessions which has not been kept alive for a while and perform any operation you need.

However, this approach won't be necessary if you only need to create some cool "logging off"-effect.

Jørgen
  • 8,820
  • 9
  • 47
  • 67
0

You can use window.onunload to fire an AJAX call when the user leaves the page/closes the tab.

window.onunload = function(){
  // AJAX call to mark user "offline"
}

EDIT: I suggest setting a variable when clicking on links, so that this only runs when the user leaves the page.

Using jQuery, it can be done like this:

$('a').click(function(){ // Run for all links
  $('body').data('linkClicked', true); // Set global variable
});

$(window).unload(function(){ // jQuery version of window.onunload
   if(!$('body').data('linkClicked')){ // Check global variable
      $.ajax({
        url: 'url',
        data: {some: data},
        async: false // this locks the browser, but it may be needed to make
                     // sure the ajax call runs before the tab is closed
      });
   }
});
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • Yep this works but be aware Sami that I think this also fires on a page change. So if the user clicks a link to another section on your site this will get fired. – Henesnarfel Feb 10 '12 at 20:57
  • Doesnt this trigger after EVERY page change? – SDZ Feb 10 '12 at 20:57
  • Yeah I thought so. How would it be done using jQuery as I don't know anything about it. – SDZ Feb 10 '12 at 20:59
  • @SamiDzHamida: Yes, I guess it does. You can set a global variable when a link is clicked on (and check for that in this function), so that it only runs when the page is closed. – gen_Eric Feb 10 '12 at 20:59
  • @SamiDzHamida: Take a look at my edit. – gen_Eric Feb 10 '12 at 21:06
  • Hmm okay thanks. When I have some time I will try and use your post. Thank you for the help. – SDZ Feb 12 '12 at 02:38
0

I would use jQuery to perform this task.. something like this and using the window.onunload as mentioned above:

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType
});
pep
  • 55
  • 1
  • 7