Questions about users ' online and offline judgments

Source: Internet
Author: User
About the user online and offline judgment problem!
The first requirement is to do a display of online user nicknames and online numbers.

Then the personal idea is to make a field in the user table to mark the user online (1) or not online (0), when the user logs on the setting of this field is 1, This field is set to 0 when the user exits through the normal channel (that is, click Secure Exit). So that the online user can be displayed on a timed refresh, in which case there is no problem with the function, but the exception is that the user does not click the security exit but directly close the browser, then this user is not always online? So my question is how to deal with whether or not he is online when the user leaves through an abnormal channel.

Please God help ~ ~ ~ ~


------Solution--------------------
A common JS file is added to each page, and one of the JS functions is responsible for sending Ajax requests to a PHP file (such as a.php) on a timed basis. Last request time by a.php each write session

For example, set every 2 minutes to send a request. On the page that needs to get the user online, read the last request time in the session, minus the last request time, if it is greater than 2 minutes, then the user is offline.
------Solution--------------------
This problem can be easily found in many cases online.
If you want to use the lightest, you can count the number of session temporary files or other storage methods to determine how much online in the current 20 minutes. Of course, this time accuracy can be reduced to a minute (with the increase in the consumption of the session recovery mechanism).
Regardless of any scenario, this function is still a lot of consumption on the server, if not necessary, it is recommended to give up.
------Solution--------------------
The way to correct ideas and not to use third-party tools such as Memcache is
Analysis: Online statistical difficulty is that there is no global sharing method
Analytic method:
The use of files to save the information of online users, so that the processing of text is actually much faster than the database (PHP database time is more than PHP read a small text time)
The format saved in the text is an array of

Return Array (
20312=>13671927362,//key value is user uid value worth user login timestamp
20313=>13671927363,
);

1: When the user logs in, gets the current timestamp, session record user ID
2: Then get the text in the file, which holds the information of the online user, to determine whether the current UID exists in this, do not exist on the insert, and then continue to save
3: Online people get, just read the file, then count out the number of people online
4: Whenever you access this file (whether you are ready to count the number of people online, or a new user joins), traverse the data in the text, determine whether the timestamp of each user is less than the current timestamp and more than half an hour, if so, then remove the user information.
Implementing the steps key points:
Get text in an array
$online =require ' xxx.php ';
Saving text in an array
$data [1111]=123748492;
File_put_contents (' xx.php ', ' ');
Buxie!
------Solution--------------------
The key to depositing a normal file is the resource monopoly issue at the time of writing.
------Solution--------------------
Reference:
each session of temporary files by Sessionid,sessionid different, the generation of temporary file directory are different.
The question I exist:
1: Can I read the session file of the Temp folder? Is it encrypted or is it plain text?
2: What if it is text? What is the content of the session? So what are those arrays, and how are the objects stored in the text? (An array can also, how is the object stored in the text?)

1, of course, can read the session temporary files. You can find them at the Session.save_path designated place.
2, session temporary file is a plain text file. is stored in a simplified serialization format, PHP already provides functions for session serialization and deserialization. You'll see for yourself.

You're all talking about how to store problems, but how to store them is not the main problem.
The key is "how to identify", since the consensus is that the "non-normal channel departure" check is required to poll. Well, then get the kind of solution out there. PK It is (assuming there are 10,000 users online)
  • 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.