NYCPHP Meetup

NYPHP.org

[nycphp-talk] Hmm, how to count 'online' users?

ophir prusak lists at prusak.com
Thu Aug 12 12:49:43 EDT 2004


Counting online users is really two problems:

1. How do you know when a user is "online"
2. How do you efficiently store the online user count.

Regarding how do you know when a user is online, there is no simple and 
accurate way of knowing how many users currently have their browser open 
and are viewing a page on the site with standard HTML pages. (yes, 
technically you could use flash/java and open a socket connection but 
that would be an extreme resource hog just for online user counts).

First of all, you need to ask yourself how YOU define an online user.
Most people define a user to be online if they've *requested* a page 
within the last X minutes. I've seen values from 2 to 30 minutes.

Regarding how to store the info, a problem like this is just asking for 
a local memory based solution, but since you said the site runs on two 
servers the simplest solution would be to use a MySQL HEAP (memory) 
table with 3 columns: username/ID, group and last seen.
Even with 40,000 people online at a time the used memory will be minimal.
You should not be updating your accounts table (which is probably very 
big) to store online info.

Then, every 5 minutes delete from table where last seen > 10 minute ago.
Instead of updating the last seen column EVERY time a logged in user 
views a page, store a cookie that says when they last told the server 
they were seen.
If it's over 5 minutes, then update the database. That way you're 
guaranteed at most one update per online user per 5 minutes.

Hope that helps,

Ophir Prusak
(I really need a sig)



Mitch Pirtle wrote:

> Daniel Convissor wrote:
>
>> Hi Mitch:
>>
>> Have a table with two columns: group_name and group_count.  Each time 
>> someone logs in, update the group_count column with group_count+1 for 
>> the appropriate group_name.  When someone logs out, do a -1.
>>  
>>
>
> And if they don't hit the 'logout' button?
>
> -- Mitch, still trying to grok session expiration notification in PHP
>
> _______________________________________________
> New York PHP Talk
> Supporting AMP Technology (Apache/MySQL/PHP)
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.newyorkphp.org


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20040812/06304ebe/attachment.html>


More information about the talk mailing list