How to Count online numbers

Source: Internet
Author: User
Tags count mysql php file php code require

In the forum, someone asked me how to count online numbers? I don't know what the best way is. The following is the implementation of the principle of the site, I wrote it for your reference. This is only my method, certainly not the best, but also hope that the experts to correct.

In fact, to really count the number of concurrent online, it is not very realistic, because the HTTP protocol is a kind of stateless protocol. When a client makes a request to the server, the server immediately establishes a new TCP/IP connection, and the connection closes after the session is complete, such as when the page is fully loaded. In general, the number of online people is defined as the number of simultaneous visits to the site over 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 visitor accesses a Web site first. He entered the address of the target website in the browser's address bar, and then continued to browse the Web page for a period of time, and finally, close the browser or enter a new URL--the browse is over. For the server side, visitors can be aware of the arrival of visitors in the page can also know, but how do you know when to go? Because the HTTP protocol is stateless, it is not known. The usual practice is to write down the time when the visitor last browsed the site page. If the visitor does not have a new action at a specific time, then you can assume that he is gone.

Based on this idea, I think it is best to use a database, because the database is more efficient than other methods such as text files. The following example is used in MySQL, and it is easy to use other types of database systems. Then, call the PHP file on all pages, updating the data on the one hand and displaying the number of people online on the other. But there is one problem--how long is it that people who visit are concurrent? In general, is half an hour, that is, 1800 seconds, specific to the site according to the situation to determine. The longer this time, the more people will be counting the number of concurrent online. This site is 15 minutes, 900 seconds. It's a good idea to represent a visitor by its IP address. In the case of dial-up Internet, the probability that two users assigned the same IP address to browse the same site in a short time is very small.

First, use the MySQL tool to build a table:

CREATE TABLE Ccol (
ID integer NOT NULL auto_increment, #记录的ID
IP char not NULL, #访问者的IP地址
Dtstamp datetime NOT NULL, #最后访问时间
Uri Char (255), #访问者请求的URI
Primary KEY (ID)
);


Then, write a section of PHP code:


?
/*
File: Ccol.php-concurrent OnLine Statistics
Objective: To count the number of simultaneous online browsing
Author: Hunte, hunte@phpuser.com
Modified: 2000-4-25
*/

$duration = 1800;
Require "db.php";
Contains Dbsql, details can refer to my another article
$ccol =new Dbsql;
$ccol->connect ();
$ccol->query ("DELETE from Ccol WHERE (Unix_timestamp ())-unix_timestamp (Dtstamp)) > $duration"); Delete records for more than half an hour
$ccol->query ("select * from Ccol WHERE ip= ' $REMOTE _addr '");
Determines whether the current IP exists in the table
if ($ccol->nf ())//have?
{
Pointer to an array of records found $ccol->next_record ()//Down
$id = $ccol->f (' id ');
$ccol->query ("UPDATE ccol SET Dtstamp=now (), uri= ' $REQUEST _uri ' WHERE id= $id");
Set 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, and the WHERE clause that follows is optional--the excess time has been removed
$ccol->next_record ()
echo "Online number:", $ccol->f (' Ccol ');
$ccol->free_result ();
?>


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

--index.php
...
<!--show online number->
<?require.. /stats/ccol.php3?>
...

Of course, there is room for improvement in this piece of code. For example, it is unnecessary and inefficient to delete a half hour record before each call. What can be done for a longer period of time, such as 6 hours. Let's think about it, I won't say it. This method as long as a little modification, you can send other uses, such as session management, Web site access statistics analysis.



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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.