This is a very accurate, through PHP to achieve statistics online number of methods, want to know how to achieve the patience to read.
<?php $filename = ' online.txt '//data file $cookiename = ' vgotcn_onlinecount ';//cookie name $onlinetime =600;//Online active time, in seconds (
That is 600 equals 10 minutes) $online =file ($filename); The PHP file () function reads the entire file into an array. Similar to 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 line breaks.
If it fails, it returns false $nowtime =$_server[' Request_time '];
$nowonline =array ();
Get the data that is still valid foreach ($online as $line) {$row =explode (' | ', $line);
$sesstime =trim ($row [1]); if ($nowtime-$sesstime) <= $onlinetime) {//If still in valid time, the data is persisted, otherwise the statistics are discarded $nowonline [$row [0]]= $sesstime;/
Get online list to array, session ID is key, last communication time is key value}}/@ Create visitor Communication State use cookie communication cookie will expire when browser is closed, but if you do not close the browser, the cookie will remain in effect until the program's online time elapses
*/if (Isset ($_cookie[$cookiename)) {//If there is a COOKIE that is not an initial visit, the number is not added and the communication time is updated $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 Current time status//statistics number online now $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. ');
Hopefully this article will help you learn about PHP programming.