First, the use of PHP to simple calculation of the current number of online Web site methods, simply by computing the visitor's IP address to obtain a general result, can not accurately calculate the current number of people online.
Header (' content-type:text/html; Charset=utf-8 ');
Author:www.scutephp.com
$entries =file ($online _log);//returns the file as an array, where each cell in the array is the corresponding line in the file, including the newline character
$temp =array ();
for ($i =0; $i
<>
$entry =explode (', ', trim ($entries [$i]));
if ($entry [0]!=getenv (' remote_addr ')) && ($entry [1]>time ())} {
}
}
$users _online=count ($temp);//Calculate the number of people online
$entries =implode (' ', $temp);
$FP =fopen ($online _log, ' W ');
Fputs ($fp, $entries);
Flock ($FP, lock_un);
Fclose ($FP);
Echo ' currently has '. $users _online. ' People online ';
Second, the use of PHP more accurate statistics on-line number of methods, note that the precise refers to the number of points, if the need for accurate in time, you need to adjust the code in accordance with the actual time of validity.
Author:www.scutephp.com
$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. Returns False if it fails
$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 will continue to be saved or discarded no longer counted
$nowonline [$row [0]]= $sesstime;//Get the online list to an array, the session ID is the key name, the last communication time is the key value
}
}
/*
@ Create Visitor Communication status
Use cookies to communicate
The cookie will expire when the browser is closed, but if you do not close the browser, the cookie will remain in effect until the online time of the program setting expires
*/
if (Isset ($_cookie[$cookiename])) {//If a COOKIE is not first accessed, no number of people are added and the communication time is updated
$uid =$_cookie[$cookiename];
}else{//If no cookie is the first visit
$vid =0;//to 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 state
Statistics now online people
$total _online=count ($nowonline);
Write Data
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. ');