Users Online - PHP
Hi there, I'm currently building a custom forum for a client. They have asked me to have a section to see who is currently online. I was just curious what is the best way to go about this? Is there a way to simply read all of the $_SESSION['user_id'] sessions that are currently being stored?
Will appreciate anyone who can shed some light on this subject. If it matters at all, there server is a windows box, running php4 and mysql4. I control it all with Plesk. Thanks. |
One approach is to use MySQL table for session management.
You can store each session in a sessions table in your forum MySQL database and clear out the "expired" sessions after a given duration to keep up to date. You can define "expiry" of a session in the database based on the cookie status or a time duration. For this approach though, you would have to do more than merely using the default PHP session handling (as in just calling "session_start()" for every page). You might have to write a session handler which incorporates the approach I mentioned. (you should probably run the "expiring" script in a cron job or whenever a page is reloaded. consider the costs/benefit of either approach). |
So... basically I create a table called... Users_online or something.
Store in there something like: User_id signin_time last_activity_time And then just update that everytime they cruise around the pages? And then set up a schedule script that runs every 25 minutes or something? Oh I suppose I could also kill the users record in the table if they choose to sign out. |
Yeah that's about it. I would also store the (encrypted) session Id and the IP address (useful for tracking guests as well as logged in users). Also you needn't use a scheduler using cron if you can run this script when a page is loaded or refreshed. But that would mean more load on the server every time a request is made.
A cron (or scheduler) job seems a better idea, but since it would not be part of the forum software itself, it might be dependent on the OS to run that scheduler. |
All times are GMT -5. The time now is 05:51 PM. |