views:

91

answers:

2

I want to, upon loading the page, store a cookie in the user's browser. Then, using AJAX, count the number of users with that cookie and send it off to a database row every 1 second or so. How is this accomplished?

A: 

Take a look at one of my posts!

http://stackoverflow.com/questions/2950355/set-session-in-database-in-php/2950504#2950504

Just when your storing the data add a timestamp to mysql and count the unique rows where the time > time()=30; for a 30 second delay.

RobertPitt
How do you account for users leaving the chat room though. I have a solution to this problem, but it can't account for users left unless they actually click the button to leave, and most users don't. EDIT: I looked at your post and it doesn't solve the issue I'm trying to solve here.
Nik
+2  A: 

Since it sounds like you're implementing a chat room, it's safe to assume there's some AJAXy polling going on (clients are checking for updates very frequently).

One approach might be to keep an active_sessions table in your database. It might look like this:

create table active_sessions (
   sess_id varchar(32) primary key,
   lastseen timestamp
);

Every time a client asks for an update, insert/update the row for their session ID, and then regularly delete any records with a timestamp older that T (for some value of T like 30 seconds or something)

Count the rows in the table to get a pretty good estimate of how many clients are still active.

timdev
Of course, if you have memcache or APC or something like that, you would do well to keep your active session list in some memory cache so you're not running an update against the DB every time the client requests an update.
timdev
The user counter is to be implemented into the AJAX script to update in near-realtime so the counter is always accurate. The current counter adds an entry on join, and deletes it on exit (but it only deletes if they click the button). Would there be a way to completely rewrite the system to either detect how many users are there using cookies, or a way to refresh the counter (session database) without actually clicking the button, if that makes any sense.
Nik
Also, regarding your post, how do we update the timestamp to prevent AFK users still in the room to be counted.
Nik
To get recent activity, add a column for the timestamp of the user's last post, and update it when they post. Being AFK can be considered being still active (say, within the past 15-30 seconds) but not having posted for some period of time.
Charles
My answer is assuming that you're polling for new posts via AJAX. If the polling is happening, the user is "present". So you just want to maintain a list of session_ids that have polled in the last X seconds. If you want to have an "AFK" state, you'd want another timestamp that only gets updated when the client makes some user-driven action (like posting).
timdev
Alright, this is what I was thinking, I just need to find a way to pass a PHP variable (session ID) to JS and back to PHP again to update the active users table. I am considering using json_encode for this, but haven't had too much experience with PHP to JS interaction.
Nik
JS doesn't need to know the session_id. Just make sure your getposts.php script calls session_start(), and then grab session_id() whenever you need it. I don't see any reason for the javascript stuff to know the session_id. It only cares about getting the updates, and getting the global user count.
timdev