Now want to get all the online users above the site, what good way to get it?
Reply content:
Now want to get all the online users above the site, what good way to get it?
Use MySQL memory table or memory NoSQL
If table, table structure: ID (self-increment ID), username (username), addtime (add Time), UpdateTime (update time)
Set the timeout time to 20 minutes. So online users are
Select COUNT () from the TBL where updatetime>= (now () -2060);
If it's NoSQL (Redis, for example), it's much easier.
Each user adds a key that can be used with the Usename hash value, and then set the expiration date for this key to 20 minutes.
When acquired, simply execute $redis->keys ("session_*"); Get all Users
Just a personal idea, for reference only.
Http://www.thinkphp.cn/topic/3217.html
Save a timestamp field to db
, and once the user has a new action, like refreshing the page and jumping to another page, update the field.
To determine whether the user is online, only need to query this field, and then compare the current timestamp, you can set a value, for example, 5 minutes, if the difference is less than 5 minutes, then judge the user online, otherwise the user is judged offline.
The master can understand that when the user requests the page, the backend responds and sends the page past, then the user and the server is not a so-called connection state, so the most convenient way is when the user makes some activities, the active back side to send messages, the equivalent of telling the server, "hello, Im still alive"
. But in fact, it's hard for you to get the real action of the user, such as an unexpected browser shutdown, a network failure, and so on.