这是一个非常精确的,通过php实现统计在线人数的方法,想知道怎么实现的请耐心阅读。
<?php $filename='online.txt';//数据文件 $cookiename='vgotcn_onlinecount';//cookie名称 $onlinetime=600;//在线有效时间,单位:秒 (即600等于10分钟) $online=file($filename); //php file() 函数把整个文件读入一个数组中。与 file_get_contents() 类似,不同的是 file() 将文件作为一个数组返回。数组中的每个单元都是文件中相应的一行,包括换行符在内。如果失败,则返回 false $nowtime=$_server['request_time']; $nowonline=array(); //得到仍然有效的数据 foreach($online as $line){ $row=explode('|',$line); $sesstime=trim($row[1]); if(($nowtime - $sesstime)$ftime){ $fline=$fuid.'|'.$ftime.\n; @fputs($fp,$fline); } flock($fp,lock_un); fclose($fp); } } echo 'document.write('.$total_online.');';
希望本文所述对大家学习php程序设计有所帮助。