Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Users online

  1. #1
    Regular Coder
    Join Date
    Sep 2009
    Location
    Calgary, Alberta
    Posts
    239
    Thanks
    47
    Thanked 3 Times in 3 Posts

    Users online

    Hello Everyone,

    I was wondering the best way to determine who is online or logged in on my website.?

    Where to start?

    using mysql, php, sessions.

    logging in and website complete.. just need to figure out how to add this function in.

    Thanks, Slayer.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Its somewhat difficult using sessions. The first thing you have to do is segregate the directory for the sessions to somewhere in your filesystem path (via .htaccess or inline)(do *not* iterate a sessions default directory as you are now accessing other virtual host's session files which is a huge issue). Then you need to iterate all of the files within the directory and poll for the atime or mtime on them to determine if they are stale. Then you need to open them, and determine which user is associated with them by pulling the data and unserializing it.

    Or you can use a database and manually handle your sessions. Then you can just issue a select. Sessions themselves can be stored in a database (via ini or function to session.save_handler) and chained to the session_start call. It is handy to do this if you don't want to write the entire session handling yourself. This will require you to track the time in some fashion though since you cannot pull the access or modified time from it.

  • #3
    Regular Coder
    Join Date
    Sep 2009
    Location
    Calgary, Alberta
    Posts
    239
    Thanks
    47
    Thanked 3 Times in 3 Posts
    would it be easier to add to the database for say..

    when a member logs in that it sets a online = 1 and 0 on logout.?

    and then echo the names of the people online for that db

    is this an easier way?

    Slayer

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Nope.
    There isn't a feasible way to detect if a user has abandoned a session or not. If you mark them as online and they abandon the session, you still need to go through all the sessions and compare them to the users in the table and update them as offline if they don't have an "active" session.
    If they logoff, then yeah that would work.

  • Users who have thanked Fou-Lu for this post:

    SlayerACC (08-16-2012)

  • #5
    Regular Coder
    Join Date
    Sep 2009
    Location
    Calgary, Alberta
    Posts
    239
    Thanks
    47
    Thanked 3 Times in 3 Posts
    Wow... this is now sounding way to complicated for what I know.

    Thanks for your time!.

    Slayer

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yep.
    If it helps, I've never used the built in sessions except for during development. I've always used my own manual session handling in PHP.
    The next thing I write I'll actually be looking at using the SessionHandlerInterface and register the sessions to a class object. If it works the way it looks that it should, I can easily interact it with the DB, but still chain it directly to the session_* family of functions and the session superglobal. That would be super handy. I never got into the use of the callable off of the session_set_save_handler before, and so I never bothered to write off of that (which would be pretty much the same, but uses individual functions for each functionality registered to it). The class interface is much closer to what I do anyways, so all I'd need to do is structure slightly differently and rename accordingly, and away I go with a register. It'll make it clearer when I set $_SESSION and query $_SESSION than when I issue $session->setvar() calls.

  • #7
    Regular Coder
    Join Date
    Sep 2009
    Location
    Calgary, Alberta
    Posts
    239
    Thanks
    47
    Thanked 3 Times in 3 Posts
    Well thanks again..

    I am amazed at the things you have in your head..

    Slayer.


  •  

    Posting Permissions

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