How does PHP count online users?

Source: Internet
Author: User
Tags php code
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, hunte@phpuser.com
Modification: 2000-4-25
*/
$ Duration= 1800;
Require "db. php ";
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.