【问题标题】:Simple PHP Unique page hit counter stops after two counts?简单的 PHP 唯一页面点击计数器在两次计数后停止?
【发布时间】:2014-09-23 16:19:00
【问题描述】:
 <?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地址

【问题讨论】:

  • 我正在响应页面上的 $value。
  • 我看不出脚本有什么问题.. 有没有想过脚本没有获得超过 2 个唯一 IP 地址...?也许脚本位于覆盖 REMOTE_ADDR 的代理、负载平衡器、缓存层后面?
  • !in_array 更改为in_array,您会看到它会增加。当然是在数据已经存在之后。
  • 所以,您需要一个独特的计数器。我不明白你为什么要继续计数。该脚本完全按照它应该做的那样做。你到底想达到什么目标?
  • 从第三台设备打开站点后,计数器($value)仍然保持在2;

标签: php unique ip-address counter pageviews


【解决方案1】:

试试这个。我添加了一个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);

?>

【讨论】:

  • 如果同一个人刷新页面两次,它不应该增加。
  • @SACHINHD 然后我可以删除它,然后使用我最初在评论中所说的内容,使用in_array。您也可以使用 cookie。
  • 两个设备的ip地址是否相同,计数器停在2?
  • 它的工作,但没有解决唯一的 IP 地址?
【解决方案2】:

如果你想要非唯一计数器,必须将 !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);

     ?>

【讨论】:

    【解决方案3】:
    <?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 语句尝试上述答案............

    【讨论】:

    • 这个新答案与我给你但未被接受的答案有什么不同?除了我的附加条件语句。
    猜你喜欢
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多