-
-
$filename='online.txt';//Data file - $cookiename='VGOTCN_OnLineCount';//Cookie name
- $onlinetime=600;//Online valid Time, unit: seconds (that is, 600 equals 10 minutes)
- $online=file($filename);
//PHP file() function reads the entire file into an array. Similar to file_get_contents(), except file() returns the file as an array. Each cell in the array is a corresponding line in the file, including newlines. If it fails, return 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 If it is still within the valid time, the data will continue to be saved, otherwise it will be abandoned and no longer counted
- $nowonline[$row[0]]=$sesstime;//Get the online list into the array, the session ID is the key name, and the last communication time is the key Value
- }
- }
/*
- @Create visitor communication status
- Use cookies to communicate
- COOKIE will become invalid when the browser is closed, but if the browser is not closed, this COOKIE will remain valid , until the online time set by the program times out
- */
- if(isset($_COOKIE[$cookiename])){//If there is a COOKIE, that is, it is not the first visit, the number of people will not be added and the communication time will be updated
- $uid=$_COOKIE[$ cookiename];
- }else{//If there is no COOKIE, it is the first visit
- $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 p>
//Count the number of people 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.'");';
- ?>
-
Copy code
|