This article mainly introduces PHP accurate statistics on the number of online methods, the need for friends can refer to the following
This is a very accurate, through the implementation of PHP statistics online number of methods, want to know how to achieve, please read patiently.
<?php $filename = ' online.txt ';//data file $cookiename = ' vgotcn_onlinecount ';//cookie name $onlinetime =600;//Online effective time in seconds ( i.e. 600 equals 10 minutes) $online =file ($filename); The PHP file () function reads the entire file into an array. Like File_get_contents (), the difference is that file () returns files as an array. Each cell in the array is the corresponding line in the file, including the newline character. If it fails, it returns false $nowtime =$_server[' request_time '); $nowonline =array (); Get data that is still valid foreach ($online as $line) {$row =explode (' | ', $line); $sesstime =trim ($row [1]); if ($nowtime-$sesstime) <= $onlinetime) {//If it is still within the valid time, the data continues to be saved, otherwise it is discarded and no longer counted $nowonline [$row [0]]= $sesstime;/ Get online list to array, session ID is key name, last communication time is key value}}/* @ Create Visitor communication status using cookie communication cookie will expire when the browser is closed, but if you do not close the browser, this cookie will remain in effect until the online time of the program setting expires */if ( Isset ($_cookie[$cookiename])) {//If there is a COOKIE that is not an initial visit, do not add the number of people and update the communication time $uid =$_cookie[$cookiename];} else{//If no cookie is the initial access $vid =0;//initialize the visitor ID do{//give the user a new ID $vid + +; $uid = ' U '. $vid; }while (Array_key_exists ($uid, $nowonline)); Setcookie ($cookiename, $uid); } $nowonline [$uid]= $nowtime;//Update the current time status//statistics now online number $total _online=count ($nowonline); WriteData if ($fp = @fopen ($filename, ' W ')) {if (Flock ($FP, lock_ex)) {rewind ($FP); foreach ($nowonline as $fuid = = $ftime) {$fline = $fuid. ' | '. $ftime. " \ n "; @fputs ($fp, $fline); } flock ($FP, lock_un); Fclose ($FP); }} Echo ' document.write ('. $total _online. ');