Home > Backend Development > PHP Tutorial > PHP simple method to count the number of people online_php skills

PHP simple method to count the number of people online_php skills

WBOY
Release: 2016-05-16 08:59:58
Original
1683 people have browsed it

the example in this article describes how to simply count the number of people online in php. share it with everyone for your reference, the details are as follows:

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

Copy after login

how to use:

save the above code as count.php where you want to use it:

include("count.php");

Copy after login

readers who are interested in more php-related content can check out the special topics on this site: "php file operation summary", "summary of php operations and operator usage", "php summary of network programming skills", "php basic syntax introductory tutorial", "summary of php office document operation skills (including word, excel, access, ppt)", " summary of php date and time usage", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation getting started tutorial" and "summary of common database operation skills in php"

i hope this article will be helpful to everyone in php programming.

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