繁体   English   中英

简单的PHP唯一页面点击计数器在经过两次计数后停止吗?

[英]Simple PHP Unique page hit counter stops after two counts?

 <?php  

 $countfile = 'counter.txt';
 $ipfile = 'ip.txt';



 function countint(){
   $ip = $_SERVER['REMOTE_ADDR'];
   global $countfile , $ipfile;


   if (!in_array($ip, file($ipfile, FILE_IGNORE_NEW_LINES))) {
   $current = (file_exists($countfile)) ? file_get_contents($countfile) : 0;
   file_put_contents($ipfile, $ip."\n", FILE_APPEND);
    file_put_contents($countfile, ++$current);

   }



  }

  countint();
    $value =file_get_contents($countfile);

  ?>

这就是count.php函数以及两个文件ip.txt和counter.txt

计数不超过2次

两次击中后,它将停止记录IP地址

试试看。 我添加了elseifin_array条件elseif (in_array...

旁注: unique匹配计数器会在1之后停止,这是有道理的,否则它将不是唯一的。

如果您想继续计数,可以尝试一下。 如果它没有按预期工作,请告诉我,我将尝试对其进行修改或完全删除答案。

<?php  

error_reporting(E_ALL);
ini_set('display_errors', 1);

 $countfile = 'counter.txt';
 $ipfile = 'ip.txt';

 function countint(){
   $ip = $_SERVER['REMOTE_ADDR'];
   global $countfile , $ipfile;


   if (!in_array($ip, file($ipfile, FILE_IGNORE_NEW_LINES))) {
   $current = (file_exists($countfile)) ? file_get_contents($countfile) : 0;
   file_put_contents($ipfile, $ip."\n", FILE_APPEND);

    file_put_contents($countfile, ++$current);

   }


   elseif (in_array($ip, file($ipfile, FILE_IGNORE_NEW_LINES))) {
   $current = (file_exists($countfile)) ? file_get_contents($countfile) : 0;
   file_put_contents($ipfile, $ip."\n", FILE_APPEND);

    file_put_contents($countfile, ++$current);

   }

  }

  countint();
    $value =file_get_contents($countfile);

?>

如果您想要非唯一计数器,则必须将!in_array更改为

<?php  

 $countfile = 'counter.txt';
 $ipfile = 'ip.txt';



 function countint(){
   $ip = $_SERVER['REMOTE_ADDR'];
   global $countfile , $ipfile;


    if (in_array($ip, file($ipfile, FILE_IGNORE_NEW_LINES))) {
    $current = (file_exists($countfile)) ? file_get_contents($countfile) : 0;
    file_put_contents($ipfile, $ip."\n", FILE_APPEND);
    file_put_contents($countfile, ++$current);

    }



   }

    countint();
     $value =file_get_contents($countfile);

     ?>
<?php  

$countfile = 'counter.txt';
$ipfile = 'ip.txt';



 function countint(){
   $ip = $_SERVER['REMOTE_ADDR'];
   global $countfile , $ipfile;


  if (!in_array($ip, file($ipfile, FILE_IGNORE_NEW_LINES))) {
  $current = (file_exists($countfile)) ? file_get_contents($countfile) : 0;
  file_put_contents($ipfile, $ip."\n", FILE_APPEND);
   file_put_contents($countfile, ++$current);

  }



   }

   countint();
  $value =file_get_contents($countfile);

   ?>

对于唯一的IP地址计数器来说效果很好...........如果您为每个页面视图增加,请尝试以上答案以及if else语句........ .....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM