Home > Backend Development > PHP Tutorial > php 统计在线人数,今日访问量,总访问量

php 统计在线人数,今日访问量,总访问量

WBOY
Release: 2016-06-20 13:03:28
Original
2362 people have browsed it

代码如下:

<?php </p>header('Content-type: text/html; charset=utf-8');<br />$online_log = "count.dat"; //保存人数的文件,<br />$timeout = 30;//30秒内没动作者,认为掉线<br />$entries = file($online_log);<br /><br />$temp = array();<br /><br />for ($i=0;$i<count($entries);$i++) {<br />$entry = explode(",",trim($entries[$i]));<br />if (($entry[0] != $_SERVER["REMOTE_ADDR"]) && ($entry[1] > time())) {<br />array_push($temp,$entry[0].",".$entry[1]."\n"); //取出其他浏览者的信息,并去掉超时者,保存进$temp<br />}<br />}<br /><br />array_push($temp,$_SERVER["REMOTE_ADDR"].",".(time() + ($timeout))."\n"); //更新浏览者的时间<br />$users_online = count($temp); //计算在线人数<br /><br />$entries = implode("",$temp);<br />//写入文件<br />$fp = fopen($online_log,"w");<br />flock($fp,LOCK_EX); //flock() 不能在NFS以及其他的一些网络文件系统中正常工作<br />fputs($fp,$entries);<br />flock($fp,LOCK_UN);<br />fclose($fp);<br /><p>echo "当前在线".$users_online."人";</p><p>?></p>
Copy after login


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