PHP Session Management, Counting Users With Cookie
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?
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.
Take a look at one of my posts!
set session in database in php
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.
精彩评论