Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
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. It is definitely not the best. I hope the experts can correct it.
In fact, 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 first look at how a visitor accesses 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, he closed the browser or entered a new website. 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, the visitor may be deemed to have left.
Based on the above idea, I think it is better to use a database because the database is more efficient than other methods such as text files. The following example uses MySQL, and it is easy to use 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 question: How long does the visitor count as concurrent? Generally, it is half an hour, that is, 1800 seconds. The specific information 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. It is a good method to express a visitor with the visitor's IP address. In the case of dial-up Internet access, the probability of two users with the same IP address browsing the same website within a short period of 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 access time
Uri char (255), # the visitor's requested URI
Primary key (id)
Then, write a piece of PHP code:
File: ccol. php-ConCurrent OnLine statistics
Objective: to count the number of concurrent online users
Author: Hunte, firstname.lastname@example.org
$ Duration= 1800;
Require "db. php ";
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to email@example.com. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
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:
and provide relevant evidence. A staff member will contact you within 5 working days.