Someone asked me in the forum how to count the number of online users? I don't know what is the best method. The following is the implementation principle of this site. I will write it for your reference. This is just my method. I'm sure it's not the best. I hope the experts will show me how to count the number of online students in the forum? I don't know what is the best method. The following is the implementation principle of this site. I will write it for your reference. This is just my method. I'm sure it's not the best. I hope the experts can correct it.
It is unrealistic to count the number of concurrent online users at the same time because HTTP 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, if the page is fully loaded, the connection will be closed. In general, the number of online users is 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 HTTP protocol.
Let's take a look at how a visitor visits 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 website for a period of time. Finally, the browser was closed or a new website was entered. On the server side, visitors can know when they arrive, and visitors can also know when they are browsing the page. but how can they know when they are going? The HTTP protocol is stateless, so it cannot be known. The common practice is to record the time when the visitor browsed the site page for the last time. If the visitor does not have any new actions within a specific period of time, he may be deemed to have left.
Based on the above idea, I feel that it is best to use a database because the database is more efficient than other methods such as text files. The following example uses MySQL and easily applies other types of database systems. Then, call the php file on all pages to update the data and display the number of online users. However, there is a title-how long does the visitor count as a concurrent visitor? Generally, it is half an hour, that is, 1800 seconds. The details should be determined based on the website situation. The longer the time, the more concurrent online users are counted. This site is 15 minutes, 900 seconds. Using a visitor's IP address to express a visitor is a good way. In the case of dial-up Internet access, the probability that two users with the same IP address will browse the same website in a short time is very small.
First, use the MySQL tool to create a table:
Create table ccol (
Id integer not null auto_increment, # Record ID
Ip char (15) not null, # visitor's ip address
Dtstamp datetime not null, # Last visit time
Uri char (255), # The URI requested by the visitor
Primary key (id)
);
Then, write a piece of PHP code:
/*
File: ccol. php-ConCurrent OnLine statistics
Objective: to count the number of concurrent online views
Author: Hunte, hunte@phpuser.com
Fixed: 2000-4-25
*/
$ Duration= 1800;
Require 'DB. php ';
// Contains DBSQL. For more information, see my other article.
$ Ccol = new dbSQL;
$ Ccol-> connect ();
$ Ccol-> query ('delete FROM ccol WHERE (UNIX_TIMESTAMP (NOW ()-UNIX_TIMESTAMP (dtstamp)> $ duration ');
// Delete records that have exceeded half an hour
$ Ccol-> query ('select * FROM ccol WHERE ip = '$ REMOTE_ADDR '');
// Determine whether the current IP address exists in the table