An example of PHP based on cookie to realize the function of counting online people

  • 2021-11-13 01:07:39
  • OfStack

In this paper, an example is given to describe the function of counting online numbers based on PHP and cookie. Share it for your reference, as follows:

online. php file:


<?php
/*
  @ PHP  Online headcount program 
  Copyright (c) www.vgot.cn by Pader 1:25 2009 Year 1 Month 7 Day 
  Homepage:http://www.vgot.cn
  QQ: 270075658
  How to use it: <script src="online.php"></script>
  note: 1 General independent online number statistics programs are all statistical online IP Number, and this is not accurate 
   For example, local area network visitors, such as companies, school computer rooms and Internet cafes, although intranets IP Different, but the external network IP It's all 1 Sample 
   If the same 1 No matter how many people visit your website in LAN, it is only considered as 1 Individual 
   This small program solves this problem. It takes computers as a single computer, and each computer counts 1 Visitors 
   Of course, because it uses COOKIE If you are with 1 Use two different core browsers to access on a computer, so don't make any difference 
*/
$filename = 'online.txt'; // Data file 
$cookiename = 'VGOTCN_OnLineCount'; //cookie Name 
$onlinetime = 600; // Online effective time in seconds  ( I.e. 600 Equal to 10 Minutes )
$online = file($filename); 
$nowtime = 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 in the valid time, the data will continue to be saved, otherwise it will be abandoned and no longer counted 
    $nowonline[$row[0]] = $sesstime; // Get online list to array, session ID Is the key name, and the last communication time is the key value 
  }
}
/*
  @  Create visitor communication status 
     Use cookie Communication 
    COOKIE  Will expire when the browser is closed, but if the browser is not closed, this  COOKIE  Will 1 Valid until the online time set by the program times out 
*/
if(isset($_COOKIE[$cookiename])) { // If there is COOKIE That is, if it is not the first visit, do not add the number of people and update the communication time 
  $uid = $_COOKIE[$cookiename];
} else { // If not COOKIE That is, the first visit 
  $vid = 0; // Initialize visitors ID
  do { // To the user 1 A new ID
    $vid++;
    $uid = 'U'.$vid;
  } while (array_key_exists($uid,$nowonline));
  setcookie($cookiename,$uid);
}
$nowonline[$uid] = $nowtime; // Update the current time status 
/*
  @  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.'");'; 
?>

According to the explanation in the comment, it can be accessed by the following js statement:


<script src="online.php"></script>

For more readers interested in PHP, please check out the topics on this site: "Summary of cookie Usage in PHP", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Grammar", "Summary of PHP Operation and Operator Usage", "Summary of PHP Network Programming Skills" and "Summary of php String (string) Usage"

I hope this article is helpful to everyone's PHP programming.


Related articles: