SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Know when user accesses/leaves a page

    I know it is extremely easy to know when a user accesses a page. The way I do it is run a query every time a user goes to this certain page. The thing I am having trouble with is finding out when the user leaves.

    I have created a chat feature to my website and am trying to get the "users online" based off on who is there at that moment updated by ajax. How do I find out a user has left the page so they are no longer counted among the "users online"? I might be posting in the wrong language forum because this might be javascript/ajax but I'm not sure.... Thanks in advance!

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Keep track of the last time the user accessed, or updated there session. After a certain amount of time has passed consider the user left.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    javascript onunload()or onbeforeunload() might help, but they not well supported, and triggered by a refresh as well as leaving the page
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  4. #4
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    You could keep sending small ajax requests to the server every few seconds. But it's not very nice and a big ol' waste of bandwidth unless you *really* need to know if they've left.

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,788
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Most scripts just work based on a time period. If the script hasn't heard from them in 5 minutes or 10 minutes the script assumes they've left.

    If they are using a browser that supports ajax you could set up the page to signal the server every minute and then if you have previously received a signal and haven't for over a minute then you know they've left.

    Doesn't stop them leaving your page open while they go off foe an hour for lunch. Just because the page is still displayed doesn't mean that anyone is looking at it.
    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="^$">

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you really wanted, you could build upon the suggestions to send an ajax request periodically. This could be combined with additional client side logic which listens for mousemove and keydown events. Going for an extended period of time without either of these events occurring could be used as a pretty decent indication that the user is not actively using the webpage, and has likely walked away/doing something else.

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah I just went with the "being inactive for 10-15 minutes make then go bye bye" code. It was just the simplest way although it doesn't count users who just idle on the page. Thanks for all the suggestions guys.

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,788
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by crmalibu View Post
    If you really wanted, you could build upon the suggestions to send an ajax request periodically. This could be combined with additional client side logic which listens for mousemove and keydown events. Going for an extended period of time without either of these events occurring could be used as a pretty decent indication that the user is not actively using the webpage, and has likely walked away/doing something else.
    You do need to allow for people who don't have JavaScript/Ajax as well though so you still need a fixed time to use for anyone where the server never receives a signal back from the page.
    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="^$">


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
  •