About the user online and offline judgment problem!

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

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

1: The landlord of the idea of a more unrestrained, is estimated to have no reference to other people's thoughts beforehand
2: The 1 floor of the answer logic does not pass. 20 users access this page at the same time? Is it 2 minutes? Each user sends a request to the server side? How many times does the server have to process? If you're going to say your server is bull B, okay! But also the premise to let the user this idiot has been stuck on a page for up to 2 minutes?

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.

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

13671927362,//key value for 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 ', '


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

PHP code   ?     123456     <?php return Array (   ...)

This is your fourth step when you visit this file? Is this file a time when I visit or when the user visits? There is the way you say the use of text to achieve online user statistics, if you have the file is every time a user login, you will visit, then you add new users to determine when all users join the time stamp and the current user's timestamp comparison, Then this will generate a lot of users in fact online but you already in the program to calculate him to be offline!
Well, if that's the file that I visited, it was when I visited the file on the page that needed to get the online user, what about the file update that stored the user's online information?
I read everyone's reply after also did not have a concrete idea!


function Counter_user_online ($temp) {$user _online = "count.txt";//Save the number of files, Touch ($user _online) in the root directory of the website;//If there is no this file, create $ Timeout = 120;//120 seconds not moving author, think drop line $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 not the user IP and the timing is not timed out then put into the array array_push ($temp, $user [0]. ",". $user [1]);}} Array_push ($temp, getenv (' remote_addr '). ",". ( Time () + ($timeout)). ' #'); Save this user's information $user_arr = Implode ("#", $temp);//write File $fp = fopen ($user _online, "w"); Flock ($fp, LOCK_EX); Flock () cannot function properly in NFS and some other network file systems Fputs ($FP, $user _arr), Flock ($FP, Lock_un), fclose ($FP); echo count ($temp);}

Degrees Niang Find, you can add a field, determine whether offline,


Reference to the 4 floor zouhao619 's reply: The right way of thinking and without the help of Memcache and other third-party tools 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

PHP code   ?       ......

1: My user access is triggered (you think, this is slag, it is best to automatically trigger ...) Think of the user (including yourself) want to see the online user, in fact, has already visited this page, so has triggered this page, cough, I said that any page must have such code, can refer to the 5th step of the way to reduce code redundancy)
2: The text of the various users and the user operation of the time stamp, (if you think this is not good, want to be more timely, you can also choose to record the last User Action page timestamp, but this will increase the pressure on the server, I think it is not necessary)
3: User update time, actually I said it before. When the user operates the page, it reads the file and determines whether the user is in this text, and if not, adds the current user ID and the current user's timestamp.
4: Delete User time. When the third step is taken, it is also judged that the time stamp of each user is half an hour apart from the current time stamp (half an hour can be determined by you at the most appropriate time) and deleted.
5: Each step of the user operation to verify the judgment, read the text file, the general design is now a single-entry form, you can define the base class, put in the constructor, so you can solve the code redundancy (every step to read the text file, do not think about the pressure is very large, in fact, reading small file pressure is not very big, When you go to the PHP page, you actually read it as well.

Implementation process I'm not going to talk about this storage method, but I recommend saving the data to a temporary memory table. Of course, this also has to consider your hardware environment.

This question can be found in many cases online.
If you want to use the lightest, count the number of session temporary files or other storage units to determine how much online you have 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).
No matter what the scenario, this function is still a lot of consumption on the server, if not necessary, it is recommended to give up.

Can I do this, because I have to make a field in the database to flag whether the user is online, then according to the number of times you count the sessions, I could log the user's ID and login time with the session, Then set the expiration time for this session is 5 hours, then I can go through the directory of the session file every 20 minutes, and then open each session file, and then analyze the contents of the file to get the user ID and logon time, Then I put the user's login time and the current time to do worse if it is greater than 5 hours (here is the limit of the user can always be online for 5 hours) then the current ID of the user in an array, and delete the session file, until the end of the traversal of all the files, The array that holds the expired user ID is created as a string and the database is updated once.
Of course, it may be that the user clicked on the "7 Days without landing", so I can add a step before the update, as we save the cookie with a fixed prefix and user ID as the name, then I asked to find a cookie, will have the ID of the cookie removed, In this way, I can implement this function very well.
Then I do not have to put a JS file on each page timed request server, just need to be in which display online user's page inside put one on it.
Is there anything wrong with my thinking? Please guide under, thank you!

Reference 5 Floor Beyondqqiang reply: Reference 4 floor zouhao619 's reply: The correct idea and does not use the memcache and so on third-party tool's method 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 formats ...
Please look at the 9 floor of my reply, and then please talk about your own ideas, please advise!

Implementation process I'm not going to talk about this storage method, but I recommend saving the data to a temporary memory table. Of course, this also has to consider your hardware environment.

Please see my reply on the 9 floor, please advise!

The key to depositing a normal file is the resource monopoly issue at the time of writing.

The key to depositing a normal file is the resource monopoly issue at the time of writing.
Gave away

Quote 3 Floor dream1206 reply: This question can find many cases on the net.
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.
Could I do this because ...
Not OK!
Each session temporary files are Sessionid,sessionid different, the generation of temporary file directories 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?)

Each session temporary files are Sessionid,sessionid different, the generation of temporary file directories 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)

Quote 3 Floor dream1206 reply: This question can find many cases on the net.
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.

Could I do this because ...
You don't need to change your original code, which is why I think statistics session is the lightest.
PHP has a GC session mechanism, which starts at a certain probability and cleans up the session beyond the configuration item Session.lifetime. This default value is 21 minutes, but this error time is greater because it is not certain when the probability will be met to start reclaiming an expired session.
You just need to count the number of files in the session folder, you don't have to read and judge.

<?php$path=session_save_path (); $path | | ($path = '/tmp '); $path. = '/sess_* '; echo "current online number:". Count (Glob ($path));

The ultimate must kill write a client ... How to control what you want.

This CS behavior on the BS site is still relatively difficult, so if you really value this scene then a client bar ...

Reference 9 Floor Beyondqqiang reply: Reference 3 floor dream1206 reply: This question can find many cases on the net.
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 feature is still a lot of consumption on the server, if not ...

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

Back to one, come back from work.

There are drawbacks.

  • 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.