Home > php教程 > php手册 > 用文本统计在线人数

用文本统计在线人数

WBOY
Release: 2016-06-21 09:05:21
Original
1142 people have browsed it

统计|在线|在线人数

此方法无需MySQL数据库支持,利用普通文本实现计数功能

//首先你要有读写文件的权限
//本程序可以直接运行,第一次报错,以缶涂梢?
$online_log = "count.dat"; //保存人数的文件,
$timeout = 30;//30秒内没动作者,认为掉线
$entries = file($online_log);

$temp = array();

for ($i=0;$i$entry = explode(",",trim($entries[$i]));
if (($entry[0] != getenv('REMOTE_ADDR')) && ($entry[1] > time())) {
array_push($temp,$entry[0].",".$entry[1]."\n"); //取出其他浏览者的信息,并去掉超时者,保存进$temp
}
}

array_push($temp,getenv('REMOTE_ADDR').",".(time() + ($timeout))."\n"); //更新浏览者的时间
$users_online = count($temp); //计算在线人数

$entries = implode("",$temp);
//写入文件
$fp = fopen($online_log,"w");
flock($fp,LOCK_EX); //flock() 不能在NFS以及其他的一些网络文件系统中正常工作
fputs($fp,$entries);
flock($fp,LOCK_UN);
fclose($fp);

echo "当前有".$users_online."人在线";

?>

使用方法:

1. 将以上代码另存为count.php

2. 在需要的页面引入计数器 即可



Related labels:
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template