简体   繁体   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);

  ?>

This is this the count.php function included along with two file ip.txt and counter.txt 这就是count.php函数以及两个文件ip.txt和counter.txt

the count is not going past 2 hits 计数不超过2次

after two hits it stops recording the ip address 两次击中后,它将停止记录IP地址

Give this a try. 试试看。 I added an elseif and in_array condition elseif (in_array... 我添加了elseifin_array条件elseif (in_array...

Sidenote: It makes sense that a unique hit counter stops after 1 , otherwise it wouldn't be unique. 旁注: unique匹配计数器会在1之后停止,这是有道理的,否则它将不是唯一的。

If you want to keep counting them, you can give this a go. 如果您想继续计数,可以尝试一下。 If it doesn't work as expected, let me know and I will try and modify it, or delete the answer altogether. 如果它没有按预期工作,请告诉我,我将尝试对其进行修改或完全删除答案。

<?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);

?>

had to change the !in_array to if you want non-unique counter 如果您想要非唯一计数器,则必须将!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);

   ?>

works fine for unique ip address counter.................... if you to increment for every page view try the above answer with if else statement............ 对于唯一的IP地址计数器来说效果很好...........如果您为每个页面视图增加,请尝试以上答案以及if else语句........ .....

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

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