Home > php教程 > php手册 > body text

php实现当前用户在线人数

WBOY
Release: 2016-06-13 10:37:53
Original
1680 people have browsed it

原理:根据不同的IP统计出当前有多少人在线。
实现方式:可以用数据库,也可以用文本。
我这里用了文本实现。
/**
*@ Date         2010.04.07
*@ Author       华夏之星 bkJia.com
*@ Blog         http://hi.baidu.com/woaidelphi/blog
*/
$user_online = "count.php"; //保存人数的文件
touch($user_online);//如果没有此文件,则创建
$timeout = 30;//30秒内没动作者,认为掉线
$user_arr = file_get_contents($user_online);
$user_arr = explode(#,rtrim($user_arr,#));print_r($user_arr);
$temp = array();
foreach($user_arr as $value){
$user = explode(",",trim($value));
if (($user[0] != getenv(REMOTE_ADDR)) && ($user[1] > time())) {//如果不是本用户IP并时间没有超时则放入到数组中
array_push($temp,$user[0].",".$user[1]);
}
}
array_push($temp,getenv(REMOTE_ADDR).",".(time() + ($timeout)).#); //保存本用户的信息
$user_arr = implode("#",$temp);
//写入文件
$fp = fopen($user_online,"w");
flock($fp,LOCK_EX); //flock() 不能在NFS以及其他的一些网络文件系统中正常工作
fputs($fp,$user_arr);
flock($fp,LOCK_UN);
fclose($fp);
echo "当前有".count($temp)."人在线";
?>

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