How to count online number

xiaoxiao2021-03-06  63

How to count online number

Someone in the forum asked me how to count online? I don't know what is the best way. Here is the principle of this site, I wrote it out for your reference. This is just my method, it is definitely not the best, I also hope that the masters will be correct.

In fact, it is a less real thing to really count the number of people in concurrent online, because the HTTP protocol is a stateless agreement. When the client issues a request to the server, the server will immediately establish a new TCP / IP connection. After the session is over, this connection is turned off after the page is fully loaded. In general, the number of online people refers to the number of people who have access to the site at a certain period of time, rather than the number of concurrent connections based on the HTTP protocol.

Let's take a look at how a visitors visit a website. He entered the address of the target website in the address bar of the browser, and then continued to browse the webpage of the site for a while, finally, close the browser or enter the new URL - browse. For the server, visitors arrive now, visitors can also know in the browse page, but how do you know when to go? Since the HTTP protocol is stateless, it cannot be known. The usual practice is to note the time of the last browsing site page at the last time. If the visitors don't have a new action in a specific time, you can think that he is gone.

Based on this idea, I think it is best to use the database because the database is higher than other methods such as text files. The following example is to use MySQL, it is easy to use other types of database systems. Then, call this PHP file in all the pages, update the data on one hand, and the number of people can display online. However, there is a problem - how is the person visited in how long it is concluded? In general, it is half an hour, which is 1800 seconds, and the specific should be determined according to the situation of the website. The longer this time, the more people who have the statistics of the coming online. This site is 15 minutes, 900 seconds. An visitor's IP address indicates a good way. In the case of dial-up Internet access, two users assigned to the same IP address browse the same website in a short time is small.

First, build a table with MySQL tools:

Create Table Ccol (ID Integer Not Null Auto_INCREMENT, ID IP Char (15) Not Null, # 的 i 地址 地址 d, # last access time uri char (255), # 访 请 请 URI PRIMARY Key (ID));

Then, write a PHP code:

$ duration = 1800; Require "db.php"; // contains dbsql, details can be referred to my article $ ccol = new dbsql; $ ccol-> connection (); $ ccol-> query ("delete from ccol WHERE (unix_timestamp (now ()) - Unix_TimeStamp (DTSTAMP)> $ duration "); // Delete more than half an hour record $ CCOL-> Query (" SELECT * from ccol where ip = '$ remote_addr' "); / / Judgment whether the current IP exists in this table ($ ccol-> nf ()) //? {$ ccol-> next_record (); // Down to find record array pointer $ ID = $ ccol-> f ('id'); $ ccol-> query ("Update Ccol Set Dtstamp = now (), URI = '$ Request_uri' where id = ID "); // Set the last access time and access page} else // No {$ ccol-> Query (" Insert Into Ccol Values ​​(0, '$ remote_addr', now () , '$ Request_uri') ");} $ ccol-> query (" Select Count (*) as ccol from ccol where (NOW_TIMESTAMP (NOW ()) - Unix_TimeStamp (DTSTAMP)) <= $ duration "); // Find In half an hour's record, the latter WHERE clause can be available - Over time has been deleted $ ccol-> next_record () Echo "Online number:", $ ccol-> f ('ccol' $ ccol-> free_Result ();?>

How to use it? Call this program above each page of the site, for example:

--index.php ... ...

Of course, this code has improved rooms. For example, at each call, it is necessary to delete a recording of half an hour, which is not necessary and reduces efficiency. Can you do a longer time, such as 6 hours. Everyone thinks it, I will not say it.

This approach can be sent to other uses, such as session management, and website access statistical analysis.

__________________

This, not saying!

转载请注明原文地址:https://www.9cbs.com/read-112922.html

New Post(0)