Count who else is on...
by The Architect
(461 views) - 1/12/04
(recorded 1/12/04 @ 2:10:24 AM)
I decided to add the "currently online" feature employed in VBulletin that shows you how many users and how many guests are "currently" browsing the site.

Took me awhile to work out the methodology for determining this statistic, here's the logic I finally chose:

Every time you view the page, it runs your through some pretty basic logic.
Are you logged in?  
Yes: Check to see if there is a record matching your user_id in the "active users" table
Yes, there is: Update it with this new IP address, delete the one that was created before I logged in
No, there isn't: Check to make sure you haven't just come back from a period of inactivity while still logged in
You have!: Insert a new row with your IP and user_id
You've been here: Update the existing row with latest time and IP

No: Check to see if there is a record for your IP address
Yes, there is: Update it with user_id = 0, and the current time
No, there isn't: Create one, user_id = 0, current time

Every five minutes, when the daily entry/musing/notes today counts are compiled, we run the following little gem of code:

$purge_time = date('YmdHis', time() - 1020);
mysql_query('DELETE FROM active_users WHERE time < '' . $purge_time . ''');

What's that do? Well, it formulates a timestamp based on the present time minus 17 minutes. Then, it runs a query that removes all entries in the "active users" table that are older than 17 minutes. Simple as that!

On the stats page, we do the following:

$online_users_count = mysql_result(mysql_query('SELECT COUNT(*) FROM active_users WHERE user_id != 0'),0);
$online_users_text = '' . $online_users_count . ' ' . choose_tenses(user,$online_users_count) . ' online';
$online_guests_count = mysql_result(mysql_query('SELECT COUNT(*) FROM active_users WHERE user_id = 0'),0);
$online_guests_text = '' . $online_guests_count . ' ' . choose_tenses(guest,$online_guests_count) . ' online';

That just goes ahead and gives us the count for guests and logged in users, and generates that nice bit of text you see. choose_tenses() is a function I wrote that looks at the input (in the second case, $online_guests_count) and determines if the value is plural or not. If it is, it appends an 's' to the word, (in the same case, 'guest') if not, it leaves it as-is.

In case you're wondering, *that* function looks like this:

function choose_tenses($word,$input)
if ($input == 1)
$new_word = $word;
} else {
$new_word = $word . 's';
return $new_word;

And that about wraps it up... Now you'll have a slightly better idea as to how many users are browsing with you. In theory, I could auto-refresh the value at the same interval I auto-refresh the ShoutBox, but that'd be more clicking and probably a bit annoying...

Questions? Comments? Praise? Do it all in the box below!

(By the way, this took about an hour and 15 minutes from concept to completion.)
Back to The Architect's journal :: Back to the journal index :: The Architect's latest entry
eek that's clever.

any chance that you could list WHO is presently active?

   [enlite (J:: M) 1/13/04 2:28 AM]

So many are so wildly paranoid that someone might see them on here, it was bad enough that there's the 'last active' link. However, yes, in the chatroom version of the shoutbox, I'll show that, just for you, Tony.

Thanks for the cleverness compliment...

I could talk about how I needed to execute that code BELOW checking for a cookie because otherwise the SESSION wasn't already established and it found nothing, but I won't. erm... anymore.

   [disillusioned (J:: M) 1/13/04 2:39 AM]


   [enlite (J:: M) 1/14/04 11:49 PM]

<-- Log in to leave a note, or create an account, if you don't already have one


Home | Editor Bios | Musings | Editor Journals

Design and concept copyright 2003, 2004 Chris Cardinal :: Content copyright its respective authors

Synapse Studios: Website Design, Custom Software Development, and Web-Based Applications

OIO Page Processed in 0.034 seconds, using ~13 queries. :: 8388607
Now playing: (At least on Dis' machine)