Online and offline user judgment problems!

Source: Internet
Author: User
Tags flock
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, 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 ~~~~


Reply to discussion (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.

1: the idea of the landlord is relatively unconstrained. it is estimated that no reference has been made to others' ideas beforehand.
The answer logic on the second floor is not enough. 20 users can access this page at the same time? In 2 minutes? Each user sends a request to the server? How many times does the server process? If you want to say that your server is awesome, okay! That server is awesome! But is there a premise that the user can stay on a page for up to 2 minutes?

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.

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

 13671927362, // The key value is the user uid value. when the user logs on to the timestamp 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!

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


PHP code
?



123456

Step 4: when accessing this file? Is this file accessed by me or by the user? In addition, if you use text to collect online user statistics, if your file is accessed whenever a user logs on, when you add a new user, you can determine that the timestamp when all users join is compared with the timestamp of the current user, in this way, many users will actually be online, but you have already calculated it offline in the program!
If your file is accessed by me, that is, when I regularly access this file on the page where I need to obtain online users, so how do you update the file that stores users' online information?

After reading the replies, I don't have a specific idea!

Function counter_user_online ($ temp) {$ user_online = "count.txt"; // the file that saves the number of people, touch ($ user_online) under the root directory of the website; // If this file does not exist, create $ timeout = 120; // The author does not change the value within 120 seconds. The author considers the disconnection $ user_arr = file_get_contents ($ user_online); $ user_arr = explode ('#', rtrim ($ user_arr, '#'); $ temp = array (); foreach ($ user_arr as $ value) {$ user = explode (",", trim ($ value )); if ($ user [0]! = Getenv ('remote _ ADDR ') & ($ user [1]> time ())) {// if the current user's IP address does not time out, put it into the array array_push ($ temp, $ user [0]. ",". $ user [1]) ;}} array_push ($ temp, getenv ('remote _ ADDR '). ",". (time () + ($ timeout )). '#'); // save the user's information $ user_arr = implode ("#", $ temp); // write the file $ fp = fopen ($ user_online, "w"); flock ($ fp, LOCK_EX); // flock () cannot work properly in NFS or some other network file systems ($ fp, $ user_arr ); flock ($ fp, LOCK_UN); fclose ($ fp); echo count ($ temp );}

You can add a field to determine whether it is offline,

Reference the reply from zouhao619 on the 4th floor: the correct idea and the method without using third-party tools such as memcache is as follows:
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


PHP code
?


......
1: I only trigger user access (you think, this is slag, it is best to automatically trigger ..... think about it. users (including yourself) who want to see online users have actually accessed this page, so they have triggered this page, I am talking about the code that must be available on any page. you can refer to Step 1 to reduce code redundancy)
2: the timestamp of each user and each user operation stored in the text, you can also choose to record the timestamp of the last user operation page, but this will increase the pressure on the server, I think it is unnecessary)
3: User update time, as I mentioned before. when the user operates the page, he reads the file and determines whether the user is in the text. if not, he adds the current user ID and the timestamp of the current user.
4: User deletion time. when the third step is performed, it also determines the difference between the timestamp of each user and the current timestamp by more than half an hour (you can decide the most appropriate time by half an hour, delete it.
5: each step of user operations must be verified and judged to read text files. Currently, the design is generally in the form of a single entry. you can define the base class and put it in the constructor, this can solve code redundancy (reading text files at each step is not stressful. In fact, reading small files is not very stressful. when you access the php page, in fact, this operation is also performed)

I will not discuss the implementation process. for this storage method, I suggest saving the data to a temporary memory table. of course, you have to consider your hardware environment.

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.

I can't do this, because I have to make a field in the database to indicate whether the user is online or not, so according to your method of counting the number of sessions, when a user logs on, he can use the session to record his id and logon time, and set the session expiration time to 5 hours, then I can traverse the directory where the session file is stored every 20 minutes, open each session file by myself, and analyze the content in the file to obtain the user ID and logon time, then, I make the difference between the user's logon time and the current time if it is more than five hours (here, it indicates that the maximum length of the user's online time is 5 hours) the user of the current id is stored in an array, and the session file is deleted until all files are traversed. the array of expired user IDs is constructed into a string and the database is updated at one time.

Of course, the user may also click "login-free within 7 days", so that I can add another step before the update, I thought we used a fixed prefix and user id as the name when saving the cookie, so I went through the cookie one by one and removed the cookie id, in this way, I can implement this function very well.


So I don't need to put a js file on each page to request the server at regular intervals. I just need to put one in the page that shows the online user.

Is there any problem with my idea? Thank you for your guidance!

Back from BeyondQqiang on the 5th floor: Back from zouhao619 on the 4th floor: the correct idea and the method without using third-party tools such as memcache is as follows:
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, format ......


Please refer to my reply on the 9th floor. then, please share your thoughts. please advise!

I will not discuss the implementation process. for this storage method, I suggest saving the data to a temporary memory table. of course, you have to consider your hardware environment.


Please refer to my reply on the 9th floor. please advise!

The key to storing common files is the exclusive resource during writing.

The key to storing common files is the exclusive resource during writing.
Just a word

Reference the reply from dream1206 on the third floor: many cases can be found on the Internet.
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.

I can't, because ......
Not feasible!
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 ?)

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)

Reference the reply from dream1206 on the third floor: many cases can be found on the Internet.
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.

I can't, because ......
You don't need to modify your original code. This is why I think session statistics are the shortest.
PHP has a GC session mechanism, which starts at a certain probability and clears sessions that exceed the configuration item session. lifetime. The default value is 21 minutes, but the error time is large because you are not sure when the probability is met to start to recycle expired sessions.
You only need to count the number of files in the session folder, so you do not have to read them one by one and then judge them.

  

The ultimate is to write a client... how to control it.

BS websites in this CS behavior are still difficult, so if you really pay attention to this scenario, come to a client...

Back to BeyondQqiang on the 9th floor: Back to dream1206 on the 3rd floor: many cases can be found online.
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 ).
This function consumes a lot of resources on the server regardless of the solution ......

Well, you have to open a session file and find that they can be read manually and the directory can be specified.

You can get back from work.

All have drawbacks

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.