How to use PHP accurate statistics online number

xiaoxiao2021-03-06  43

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 do not have new actions 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 count the quarter 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 period of time is small.

First, build a table with MySQL tools:

CREATE TABLE CCOL

ID Integer Not Null Auto_Increment, # recorded ID

IP char (15) Not null, # 访 的 i i 地址 地址

DTSTAMP dateTime Not Null, # last access time

URI CHAR (255), # visitor requested URI

Primary Key (ID)

);

Then, write a PHP code:

/ *

File: ccol.php - Concurrent Online Statistics

Objective: Statistically the number of people browsing online

Author: Hunte, hunte@phpuser.com

Modify: 2000-4-25

* /

$ DURATION = 1800;

Require "db.php";

/ / Contain dbsql, please refer to my other article for details

$ ccol = new dbsql;

$ ccol-> connect ();

$ ccol-> query ("delete from ccol where (unix_timestamp (now ()) - UNIX_TIMESTAMP (DTSTAMP)> $ duration");

/ / Delete more than half an hour

$ ccol-> query ("SELECT * from ccol where ip = '$ remote_addr');

/ / Judgment whether the current IP exists in this table

IF ($ ccol-> nf ()) //?

{

$ ccol-> next_record (); // Down to find a pointer to the record array

$ 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 (unix_timestamp (now ()) - UNIX_TIMESTAMP (DTSTAMP)) <= $ duration");

// Find the record in half an hour, the latter WHERE clause can be available - exceeded time has been deleted

$ ccol-> next_record ()

Echo "Number of online:", $ ccol-> f ('ccol');

$ ccol-> free_result ();

?>

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

--index.php

...

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

New Post(0)