【问题标题】:Count unique page visits without counting refresh as page visit计算唯一页面访问量而不将刷新计为页面访问量
【发布时间】:2018-06-11 12:07:36
【问题描述】:

我正在为我的网站制作访客计数器,但它会在页面刷新时计数。我只想计算唯一用户访问。

             $logfile = "visitors.txt";

           if (file_exists($logfile)) {

           $handle = fopen($logfile, "r");
            $log = fread($handle, filesize($logfile));
           fclose($handle);
            } else {
              die ("The log file doesn't exist!");
            }

         `Seperate each logline`
         $log = explode("\n", trim($log));


          // Seperate each part in each logline//
          for ($i = 0; $i < count($log); $i++) {
          $log[$i] = trim($log[$i]);
          $log[$i] = explode('|', $log[$i]);
          }                  

【问题讨论】:

    标签: php counter visitor-pattern


    【解决方案1】:

    有检测页面是否刷新的代码:

    if (isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0') {
        //Code to execute when page refresh.
    } else {
        //Code to execute when page not refresh.
    }
    

    如果返回true,则页面刷新,否则不刷新。

    编辑:试试这个

    $pageRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
    if (!$pageRefreshed) {
        $count = (string) $getvisitorcount[0]["count"];
        $noZeros = 7;
        $displayZeros = 7 - strlen($count);
        $visitor = "";
        for ($i = 1; $i <= $displayZeros; $i++) {
            $visitor .= (string) 0;
        }
    }
    

    【讨论】:

    • 是的,它返回的值是 'True'
    • 表示页面已刷新。因此,在条件下编写代码,使其仅在页面未刷新时执行。
    • 这是一段代码,$count = (string)$getvisitorcount[0]["count"]; $noZeros = 7; $displayZeros = 7-strlen($count); $访问者=“”; for($i=1;$i
    • 这段代码什么时候执行?页面刷新与否。
    • 我已经在页脚添加了这段代码,所以我想根据唯一的 IP 地址只计算一个计数,但是现在当我刷新页面时,计数增加了 1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-28
    相关资源
    最近更新 更多