Online and offline user judgment problems! The first requirement is to display the online user nickname and number of online users. My personal idea is to make a field in the user table to mark the user online (1) or offline (0). when the user logs on, set this field to 1, when you exit through a normal channel (I .e. click secure exit), set this field to 0. in this way, you can determine online and offline user issues!
The first requirement is to display the online user nickname and number of online users.
My personal idea is to make a field in the user table to mark the user online (1) or offline (0). when the user logs on, set this field to 1, when you exit through a normal channel (I .e. click secure exit), set this field to 0. in this way, the online user can be refreshed on a regular basis. in this case, the function is normal, but the exception is that the user directly closes the browser without clicking secure exit, so will this user be online forever? So my problem is how to deal with it. when a user leaves through an abnormal channel, it can also detect whether the user is online.
Please help me ~~~~
------ Solution --------------------
A common js file is added to each page, and a js function in it is responsible for regularly sending ajax requests to a php file (such as a. php. The last request time is written to the session by a. php each time.
For example, a request is sent every 2 minutes. On the page where you need to obtain whether the user is online, read the last request time in the session. the current time minus the last request time. if the time exceeds 2 minutes, the user is offline.
------ Solution --------------------
This problem can be easily found on the Internet in many cases.
If you want to use the shortest tool, you can calculate the number of temporary session files or the number of units stored in other storage methods to determine the number of online files in the current 20 minutes. Of course, the time accuracy can be reduced to one minute (the consumption of the session recovery mechanism increases accordingly ).
No matter whether you adopt any scheme, this function still consumes a lot of resources on the server. if not necessary, we recommend that you give up.
------ Solution --------------------
Which of the following methods does not use third-party tools such as memcache?
Analysis: the difficulty in online statistics is that there is no global sharing method.
Resolution:
The file is used to store the information of online users. in this way, the text processing is actually much faster than the database (the php database connection time exceeds the time for php to read a small text)
The format saved in the text is array. the format is
Return array (
20312 => 13671927362, // The key value is the user uid value. The value is the timestamp when the user logs on.
20313 => 13671927363,
);
1: When a user logs on, he/she obtains the current timestamp and the session record user ID.
2: then obtain the file in the text, which stores the information of online users, judges whether the current uid exists in it, inserts it if it does not exist, and then continues to save
3: obtain the number of online users, read the file, and count the number of online users.
4: when accessing this file (whether you are preparing to count the number of online users or adding new users), you can 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 yes, remove the user information.
Key points of implementation steps:
Get text in array
$ Online = require 'XXX. php ';
Save text as an array
$ Data [1111] = 123748492;
File_put_contents ('XX. php ',' ');
Thank you!
------ Solution --------------------
The key to storing common files is the exclusive resource during writing.
------ Solution --------------------
Reference:
The sessionId is used to generate temporary file directories for each session.
My questions:
1: Can I read session files in a temporary folder? Is it encrypted? Or plain text?
2: What is text? What is stored in the session? So how are the arrays and objects stored in text? (Array is also acceptable. how does an object store text ?)
1. of course, you can read session temporary files. You can find them at the place specified by session. save_path.
2. the session temporary file is a plain text file. It is stored in a simplified serialization format. php already provides session serialization and deserialization functions. You can see it by yourself.
You are all discussing how to store data. In fact, how to store data is not the main problem.
The key lies in "how to identify". since we all agree that the "abnormal Channel exit" check requires polling. Let's get all the solutions to the primary key (assuming there are 10 thousand online users)