【问题标题】:Where to read the visitor info在哪里阅读访客信息
【发布时间】:2013-03-28 16:54:42
【问题描述】:

更新

这个问题暴露了visitors count 的过时、最糟糕的方法,每个人都应该避免使用它。使用复杂的计数器。

【问题讨论】:

标签: php count


【解决方案1】:

由于我没有找到令人满意的“足够简单”的解决方案,所以我想出了自己的解决方案。创建一个名为 ip.txt 的空文件并在代码中的某处使用它:

$ip_all = file("ip.txt");
$ip_cur = $_SERVER['REMOTE_ADDR']."\n";
if (!in_array($ip_cur, $ip_all)) {
    $ip_all[] = $ip_cur;
    file_put_contents("ip.txt", implode($ip_all));
}

echo "visitors: " . count($ip_all);

请注意,随着时间的推移,此文件可能会变得有些大,具体取决于您获得的访问者数量,因为条目不会过期并且会像 cookie 一样被删除。但正如已经提到的,我希望它尽可能简单并且不在乎。此外,我不想依赖 cookie,因为我怀疑网络爬虫和其他机器人会将它们发回。

【讨论】:

    【解决方案2】:

    您通常可以通过$_SERVER['REMOTE_ADDR'] 变量获取 IP 地址。

    【讨论】:

      【解决方案3】:

      简单:

      <?php
      $cookie_name = 'counter';
      $file = 'count.txt';
      
      if (!isset($_COOKIE[$cookie_name])) {
          $count = strval(file_get_contents($file));
          file_put_contents($file, $count + 1);
          setcookie($cookie_name, "Checked", time() + 111400);
      }
      ?>
      

      【讨论】:

        【解决方案4】:

        你好,这是我用来注册访客 ip 的。

        function get_IP() {
        
            // ADRES IP
            if     (getenv('HTTP_CLIENT_IP'))       $ipaddress = getenv('HTTP_CLIENT_IP');
            else if(getenv('HTTP_X_FORWARDED_FOR')) $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
            else if(getenv('HTTP_X_FORWARDED'))     $ipaddress = getenv('HTTP_X_FORWARDED');
            else if(getenv('HTTP_FORWARDED_FOR'))   $ipaddress = getenv('HTTP_FORWARDED_FOR');
            else if(getenv('HTTP_FORWARDED'))       $ipaddress = getenv('HTTP_FORWARDED');
            else if(getenv('REMOTE_ADDR'))          $ipaddress = getenv('REMOTE_ADDR');
            else                                    $ipaddress = 'UNKNOWN';
            //
            return $ipaddress;
        }
        

        【讨论】:

        • 我很好奇这段代码的准确性如何?有阅读材料的链接吗?
        【解决方案5】:

        您可以通过使用 w+ 打开文件来节省一些代码,它会自动为您创建它。

        <?php
        // Inits
        $file = "/tmp/counts.html";
        $cookie_namee='mycounterr-456';
        
        // File, created if !exists
        $fh = fopen($file, 'w+');
        // Get the count, 0 if the file is empty or just created
        $count = (int)fgets($fh);
        
        //if cookie isn't already set,then increase counts 
        //by one + save ip, and set a cookie to pc...
        if (!isset($_COOKIE[$cookie_namee])) {
            // Increment and write the new count
            fwrite($fh, ++$count);
            setcookie($cookie_namee, "Checked", time() + 111400);
        }
        
        fclose($fh);
        

        如果您想要一种非常简单的方法来通过 IP 或其他方式强制执行计数,您必须查看 Redis

        【讨论】:

          【解决方案6】:

          如果您希望它在您的页面上显示访问者 把它放在你的代码下。

          <?php
          
          include ("counts.html")
          
          ?> 
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-01-13
            • 2011-10-18
            • 1970-01-01
            • 2014-03-28
            • 2020-02-28
            • 2016-02-13
            • 1970-01-01
            相关资源
            最近更新 更多