View Full Version : Users online

08-16-2012, 06:08 PM
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.

08-16-2012, 06:32 PM
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.

08-16-2012, 06:40 PM
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?


08-16-2012, 08:02 PM
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.

08-16-2012, 08:09 PM
Wow... this is now sounding way to complicated for what I know.

Thanks for your time!.


08-16-2012, 08:15 PM
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.

08-16-2012, 08:26 PM
Well thanks again..

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


EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum