计算唯一页面访问次数,不计算刷新为页面访问

时间:2018-06-11 12:07:36

标签: php counter visitor

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

             $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]);
          }                  

1 个答案:

答案 0 :(得分:0)

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

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;
    }
}