Detecting when browser is closed / session ended

Hey guys,

What would be the best way to detect if a user has closed the session with my site? I need to update a database table field for a user when he or she exits the browser. Kind of like an online offline system.

To log off, they can either click log off (which is easy to detect), or close their browser (assuming they didn’t click “stay logged in” upon logging in) which is hard to detect. So the latter is what I need help with.

Thx!

There is no way to tell the difference between them closing the browser and simply leaving your site.

The only way to detect when either of those occurs (without being able to tell which one) is to use an ajax script in the page that polls the server at set time intervals (eg. one minute), then when the appropriate period has passed without the server being polled you know they have left your site.

Of course that only works if they have JavaScript enabled - there’s no way to tell otherwise.

Most servers are configured to cancel a session after a set amount of time if it isn’t cancelled prior to that - a common setting is 2 hours meaning that anyone still logged in after 2 hours would be asked to log in again.

You could use onunload, but that’s not totally reliable and still won’t completely achieve what you’re looking to do. Plus, it’s client side so you’d still need ajax.

You could try using sessions and cron. Set a session (or cookie) when a user logs in and every time they access a page you could update a mysql table with the user ID and time of access. Then run a cron job every x amount of minutes or hours and check that table. If you detect dates past a certain number of minutes or hours then deem them offline and update the table.

Just be careful with this on a busy site. Make it as efficient as possible to avoid bottlenecks. You’d probably want to use innodb for the table.

onunload only runs once the page actually starts unloading so the ajax script would likely already be gone before the script execution got that far.

onbeforeunload is a proprietary event that runs before the page starts unloading in those few browsers that support it but as it is mostly used to produce those annoying “are you sure you want to leave” messages a lot of people disable it even in the browsers that do support it. Even then you’d still need ajax and so to get it to work in the browsers that don’t have onbeforeunload you’d need to use some form of polling.

onunload is okay to use with ajax BUT the ajax call has to be synchronous. The ajax call would be gone if it was asynchronous.

Depending on how fast the page unloads the script may or may not get as far as the code to make the ajax call - whether that call is synchronous or asynchronous makes no difference as the page will definitely have unloaded before the server could respond.

Here is how I have dealt with this in the past.

First you have to have a MySQL table, lets call it Sessions (with at least a session_id column and a last_activity_date column, you can add additional columns)
Next you INSERT a record into this table storing the session id of the user session_id() and a date/time stamp (using MySQL function NOW()).
As the browse your site you update that record’s last_activity_date column.
At the same time, you have either a cronjob running or a second query run on every page that queries the Sessions table checking for a date/time stamp that is more than X minutes ago.
Example: session_id 123456 last_activity_date reports 2012-10-04 11:00:00 and it is now 2012-10-04 11:15:00, so 15 minutes have passed with no interaction.
Now do whatever you wanted to do with those records.