Home > Backend Development > PHP Tutorial > Get the number of people online in php_PHP tutorial

Get the number of people online in php_PHP tutorial

WBOY
Release: 2016-07-13 17:23:46
Original
1164 people have browsed it


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


dtstamp datetime not null, #Last access time
uri char(255), #URI requested by the visitor
primary key (id)
);

Then, write a paragraph PHP code:


/*
File: ccol.php - ConCurrent OnLine statistics
Purpose: Count the number of people browsing online at the same time
Author: Hunte, hunte@phpuser. com
Modified: 2000-4-25
*/

$duration=1800;
require "db.php";
//Contains DBSQL, please refer to mine for details Another article
$ccol=new dbSQL;
$ccol->connect();
$ccol->query("DELETE FROM ccol WHERE (UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(dtstamp) )>$duration");
//Delete records older than half an hour
$ccol->query("SELECT * FROM ccol WHERE ip=$REMOTE_ADDR");
//Determine whether the current IP Exists in this table
if ($ccol->nf())//Yes?
{
$ccol->next_record();//Move down the pointer of the found 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))//Find the records within half an hour, the following WHERE clause is optional - - Those that have exceeded the time have been deleted
$ccol->next_record()
echo "Number of people online:", $ccol->f(ccol);
$ccol->free_result();
?>

How to use it? Call this program on every page of the site, for example:
--index.php
...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template