SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Addict svcghost's Avatar
    Join Date
    Oct 2010
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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!

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    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.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by tgavin View Post
    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.
    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.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    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.

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by mrmoo View Post
    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.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,069
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by svcghost View Post
    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!
    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.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •