Checking if user is online

Discussion in 'PHP' started by Thorlax402, Aug 18, 2010.

  1. #1
    I have been trying all day to figure out how to tell which of my users is online and I have not been able to wrap my head around it. I have created a custom login system with all the usual fields (username, password, email, ip, etc) and I really want to be able to display who is on the website at any given time.

    The preferable option would be to have a real-time display of this showing, but if it is significantly easier then displaying who has been online in the last 30 minutes (time it takes for the session to expire) would be fine as well. Either way, I haven't been able to figure out how to check for connections to my website.
     
    Thorlax402, Aug 18, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Add a column to your db structure for e.g. 'online'

    Then on your login page simply do an UPDATE query setting the 'online' to 1, and then on your logout page simply do an UPDATE query setting the 'online' to 0.

    Now you can check whos online by using the WHERE clause (WHERE online = 1)
     
    danx10, Aug 18, 2010 IP
  3. Thorlax402

    Thorlax402 Member

    Messages:
    194
    Likes Received:
    2
    Best Answers:
    5
    Trophy Points:
    40
    #3
    Since users aren't required to login when their session or cookie is still active, then that would say they are online potentially indefinitely which is not really helpful. There has to be some way to do it since there are tons of cms scripts that have features like this, but I just don't know how.
     
    Thorlax402, Aug 18, 2010 IP
  4. Narrator

    Narrator Active Member

    Messages:
    392
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    80
    #4
    I usually add a column 'lastAction' and update it with a timestamp everytime the user loads a page. Then as long as the users' lastAction is within say 5 minutes they are considered online. When the user logs off I update the lastAction to 0 or anytime earlier than 5 minutes.

    I know thats not the most efficient way to do it, but I haven't found a better way yet.

    Cheers!
     
    Narrator, Aug 18, 2010 IP
  5. Thorlax402

    Thorlax402 Member

    Messages:
    194
    Likes Received:
    2
    Best Answers:
    5
    Trophy Points:
    40
    #5
    That is definitely a viable option, but if possible I'd like to avoid running a query on every page load.
     
    Thorlax402, Aug 18, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    I'd suggest what Narrator suggested as thats probably the best solution for this kind of thing.
     
    danx10, Aug 18, 2010 IP
  7. Narrator

    Narrator Active Member

    Messages:
    392
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    80
    #7
    You could use that option along with a cookie/session with the timestamp, and only query an update every time it expires. That way if the user loads a page after the 5 minutes they will stay online.
     
    Narrator, Aug 18, 2010 IP
  8. Thorlax402

    Thorlax402 Member

    Messages:
    194
    Likes Received:
    2
    Best Answers:
    5
    Trophy Points:
    40
    #8
    I'll work on trying to find little way's here and there to limit the number of queries that need to be run, but thanks for the overall idea. So far it's working perfectly.
     
    Thorlax402, Aug 19, 2010 IP