PHP如何统计在线人数
统计|在线|在线人数
在论坛里有人问我如何统计在线人数?我也不知道什么是最好的方法。下面是本站的实现的原理,我把它写出来,供大家参考。这只是我的方法,肯定不是最好的,还希望高手们予以指正。
其实,要真正统计同时在并发在线的人数,是一件不太现实的事,这是因为HTTP协议是种无状态的协议。当客户端向服务器发出一个请求时,服务器会马上建立一个新的TCP/IP连接,在该会话结束后,如页面完全载入后,这个连接就关闭了。一般来说,在线人数指的定是在一定时间段内同时访问站点的人数,而不是基于HTTP协议的并发连接数。
让我们先来看看一个访客是如何访问一个网站的。他在浏览器的地址栏里输入了目标网站的地址,然后在一段时间内持续浏览该网站的网页,最后,关闭浏览器或输入新的网址——浏览结束了。对于服务器端来说,访客到来是可以知道的,访客在浏览页面也是可以知道的,可是怎么知道什么时候走的呢?由于HTTP协议是无状态的,所以无法知道。通常的做法是记下访客最后一次浏览站点页面的时间。如果该访客在一个特定的时间内没有新的动作,那么可以认为他走了。
根据上面的这个思路,我觉得最好用数据库,因为数据库要比其他方法如文本文件的效率要高。下面的例子是使用MySQL的,很容易使用其他类型的数据库系统。然后,在所有的页面中调用这个PHP文件,一方面更新数据,另一方面可以显示在线的人数。但是,有一个问题--到底在多长时间内访问的人算是并发的呢?一般来说,是半个小时,也就是1800秒,具体的要根据网站的情况来确定。这个时间越长,统计出的并发在线的人数就越多。本站的是15分钟,900秒。用访问者的IP地址表示一个访问者是个不错的方法。在拨号上网的情况下,被分配了相同IP地址的两个用户在短时间内浏览同一个网站的概率是很小的。
首先,用MySQL的工具建一个表:
CREATE TABLE ccol(
id integer not null auto_increment, #记录的ID
ip char(15) not null, #访问者的IP地址
dtstamp datetime not null, #最后访问时间
uri char(255), #访问者请求的URI
primary key (id)
);
然后,写一段PHP代码:
/*
文件:ccol.php - ConCurrent OnLine statistics
目的:统计同时在线浏览的人数
作者:Hunte, hunte@phpuser.com
修改:2000-4-25
*/
$duration=1800;
require "db.php";
//包含DBSQL,详情可以参考我的另一篇文章
$ccol=new dbSQL;
$ccol->connect();
$ccol->query("DELETE FROM ccol WHERE (UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(dtstamp))>$duration");
//删除超过半小时的记录
$ccol->query("SELECT * FROM ccol WHERE ip='$REMOTE_ADDR'");
//判断当前的IP是否在该表中存在
if ($ccol->nf())//有?
{
$ccol->next_record();//下移找到的记录数组的指针
$id=$ccol->f('id');
$ccol->query("UPDATE ccol SET dtstamp=now(), uri='$REQUEST_URI' WHERE id=$id");
//设置最后访问时间和访问页面
}
else//没有
{
$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))//找出在半个小时内的记录,后面的WHERE子句可有可无--超出时间的已经被删除了
$ccol->next_record()
echo "在线人数:", $ccol->f('ccol');
$ccol->free_result();
?>
怎么用呢?在站点的每个页面的上面调用这个程序,举例来说:
--index.php
...

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

In this article, we will show you how to reorder multiple columns in PowerQuery by dragging and dropping. Often, when importing data from various sources, columns may not be in the desired order. Reordering columns not only allows you to arrange them in a logical order that suits your analysis or reporting needs, it also improves the readability of your data and speeds up tasks such as filtering, sorting, and performing calculations. How to rearrange multiple columns in Excel? There are many ways to rearrange columns in Excel. You can simply select the column header and drag it to the desired location. However, this approach can become cumbersome when dealing with large tables with many columns. To rearrange columns more efficiently, you can use the enhanced query editor. Enhancing the query

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

The timestamp refers to the total number of seconds from 00:00:00 on January 1, 1970, Greenwich Mean Time (08:00:00 on January 1, 1970, Beijing time) to the present. Various versions of MySQL are deployed in the production environment, including three major versions of MySQL 5.5/5.6/5.7 and N minor versions. Due to the poor upward compatibility of MySQL, the same SQL behaves differently in different versions. As follows The timestamp data type is introduced in detail from several aspects. Timestamp data access In the above three major versions of MySQL, the value range of the default timestamp (Timestamp) type is’1970-01-0100:00:01&r

ReactQuery database plug-in: Methods to implement data import and export, specific code examples are required. With the widespread application of ReactQuery in front-end development, more and more developers are beginning to use it to manage data. In actual development, we often need to export data to local files or import data from local files into the database. In order to implement these functions more conveniently, you can use the ReactQuery database plug-in. The ReactQuery database plugin provides a series of methods

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

How to use the Where method in Laravel collection Laravel is a popular PHP framework that provides a wealth of functions and tools to facilitate developers to quickly build applications. Among them, Collection is a very practical and powerful data structure in Laravel. Developers can use collections to perform various operations on data, such as filtering, mapping, sorting, etc. In collections, the Where method is a commonly used method for filtering the collection based on specified conditions.
