How does PHP know to get so current online users

Source: Internet
Author: User
Tags add time
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.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.