tags:

views:

89

answers:

4

Is there a good PHP tutorial that will show you how to display how many members are online?

I'm looking for a tutorial to design my own script.

How many people are currently online on the given site?

A: 

If you use a in-house user managing framework, simply create a new SQL table called 'online' and add a new entry each time a user logs in, and remove the entry each time he logs out.

However, if you are using something to manage users that is not in-house built, you better ask directly to the developers of the script.

If you are using phpBB, you better check the extensive documentation available at http://www.phpbb.com/

xav0989
The problem with that is when people close their browsers without explicitly "logging out"---you'll have "online" users stacking up like crazy.
brianreavis
I could add a clause where each time a page is rendered where the user count is rendered, if the latest action is older than, let's say 15 minutes, the user becomes invisible. He is logged in put not online.
xav0989
+1  A: 

Since people are lazy and frequently often forget to log out, you'd be better looking at something to do with last activity in a session, eg a page view, a form submission and so on. You could as an example store the session ID against a date/time stamp, and then each time a page is loaded, update this table with the current date and time, for the session ID of the person viewing it.

Then you can query against it, for example, finding people online in the last hour. As long as your session ID can relate to a member's ID (via cookies or however you implement it), then you should be good to go.

richsage
In other words he should simply count the sessions in the system. E.g. with a simple select count where on the session table.
Zed
Right on. One minor thing: don't forget the part about clearing items that have expired (entries with the timestamp being older than an hour). That table'd get awfully massive and inefficient otherwise (depending on the scale of the site). The clearing procedure's probably best triggered with a cron job.
brianreavis
GOod point @brianreavis - gets very unmaneagable otherwise. I'd forgotten about that crucial part! ;-) You could also do this every time a page is refreshed, although a cron job would be preferable. @Zed - the counting would only work if the expiry and clearing happened...
richsage
A: 

If you do not refrain from deploying heavy artillery, you can use a simple javascript to send empty requests to a specific url periodically. On this url you only need to check the number of requests sent from different users (e.g. hosts) to have this counter.

As I said this is heavy artillery, but saves you from problems like users not logging out, etc.

Zed
A: 

The way I handle online users status is with an "online" mysql table, when a user logs into my system I add a new entry with there information to the online users table, then I store the current time into a session variable for that user, on every page load, I calculate the time that has passed since the last time I updated there time in the online table. FOr example if 5 minutes has passed then I will save the new time to the db otherwise I will do nothing.

When a user logs out, I will remove there record from the DB and also I will run a cron job on a script to clear out any users not active in the past X amount of minutes

jasondavis