Home  >  Article  >  Backend Development  >  PHP realizes the number of current users online_PHP tutorial

PHP realizes the number of current users online_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:39:211008browse

Principle: Count how many people are currently online based on different IPs.
Implementation method: You can use database or text.
I used text implementation here.
/**
*@ Date 2010.04.07
*@ Author                 China Star bkJia.com
*@ Blog http://hi.baidu.com/woaidelphi/blog
*/
$user_online = "count.php"; //File to save the number of people
touch($user_online);//If this file does not exist, create
$timeout = 30; //If there is no action within 30 seconds, it will be considered offline
$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())) {//If it is not the user’s IP and the time has not expired, put it into the array
array_push($temp,$user[0].",".$user[1]);
}
}
array_push($temp,getenv(REMOTE_ADDR).",".(time() + ($timeout)).#); //Save this user's information
$user_arr = implode("#",$temp);
//Write to file
$fp = fopen($user_online,"w");
flock($fp,LOCK_EX); //flock() does not work properly in NFS and other network file systems
fputs($fp,$user_arr);
flock($fp,LOCK_UN);
fclose($fp);
echo "Currently there are".count($temp)."People are online";
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486320.htmlTechArticlePrinciple: Count how many people are currently online based on different IPs. Implementation method: You can use a database or text. I used text implementation here. ?php /** *@ Date 2010.04.07...
Statement:
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