Someone asked me in the forum how to count the number of people online? I also don't know what is the best way. The following is the implementation principle of this site. I wrote it out for your reference. This is just my method, definitely not the best, and I hope experts can correct me.
In fact, it is unrealistic to truly count the number of people online at the same time. This is because the HTTP protocol is a stateless protocol. When the client sends a request to the server, the server will immediately establish a new TCP/IP connection. After the session ends, such as when the page is fully loaded, the connection is closed. Generally speaking, the number of people online refers to the number of people who visit the site at the same time within a certain period of time, rather than the number of concurrent connections based on the HTTP protocol.
Let’s first take a look at how a visitor accesses a website. He enters the address of the target website into the address bar of the browser, then continues to browse the website's pages for a period of time. Finally, he closes the browser or enters a new URL - the browsing is over. For the server side, you can know when a visitor has arrived, and you can also know when the visitor is browsing the page, but how do you know when you leave? Since the HTTP protocol is stateless, there is no way to know. Common practice is to note the last time a visitor viewed a page on your site. If the visitor has no new actions within a specific period of time, he can be considered gone.
Based on the above idea, I think it is best to use a database, because the database is more efficient than other methods such as text files. The examples below are using MySQL, but can easily be used with other types of database systems. Then, call this PHP file in all pages to update the data on the one hand and display the number of people online on the other hand. However, there is a question - how long does it take for people to visit concurrently? Generally speaking, it is half an hour, which is 1800 seconds. The specific time will be determined according to the situation of the website. The longer this time is, the more number of concurrent online people will be counted. This site is 15 minutes, 900 seconds. A good way to represent a visitor is by their IP address. In the case of dial-up Internet access, the probability that two users assigned the same IP address will browse the same website in a short period of time is very small.
First, use MySQL tools to create a table:
CREATE TABLE ccol(
id integer not null auto_increment, #Record ID
ip char(15) not null, #Visitor’s IP address