傳統的網頁訪問統計,已經有很多,如 51la、百度統計、站長統計
一般都需要引用JS,在你的網頁內嵌入JS,這個操作存在風險,並且不可控。
可以考慮使用 【img src、css src、link href】引用統計;
使用方法:
在需要統計頁面添加 <img src="/stat.php" />
訪問記錄按照站點每日存儲,記錄內容:time、ip、url、user_agent、統計參數(stat.php?xxxx)
使用 stat.php?null=1 禁止輸出svg
使用 stat.php?host=xxx 將多個不同站點統計到一起
后期基於記錄內容可以分析訪問數據;
統計代碼的PHP實現:
<?php error_reporting(E_ALL & ~E_NOTICE); define('ROOT_PATH', dirname(__FILE__) . '/'); $now = date('Y-m-d H:i:s'); $host = '_null'; $url = @$_SERVER['HTTP_REFERER'] ?: ''; if (!empty($url)) { $arr = parse_url($url); $host = $arr['host']; } if (!empty($_GET['host'])) { $host = trim($_GET['host']); $host = str_replace(['.', '/', '\\', ' '], '', $host); } $file = ROOT_PATH . '/stat/' . $host . '/stat.log'; $str = @file_get_contents($file); $arr = []; if (empty($str)) { mkdir(ROOT_PATH . '/stat/' . $host); $arr = [0,0,$now]; } else { $arr = explode(',', $str); } $arr[0]++; $arr[1] = substr($arr[2], 0, 10) < date('Y-m-d') ? 0 : ($arr[1] + 1); $arr[2] = $now; @file_put_contents($file, implode(',', $arr)); $data = [ 'time' => $now, 'ip' => $_SERVER['REMOTE_ADDR'], 'user_agent' => @$_SERVER['HTTP_USER_AGENT'] ?: '', 'url' => $url, 'query' => @$_SERVER['QUERY_STRING'] ?: '', ]; @file_put_contents(ROOT_PATH . '/stat/' . $host . '/' . date('Ymd') . '.log', json_encode($data) . PHP_EOL, LOCK_EX|FILE_APPEND); if (!empty($_GET['null'])) { exit; } // 輸出svg $out = $arr[0] . '/' . $arr[1]; $w = strlen($out) * 7 + 5; header('Content-Type:image/svg+xml'); echo <<<EOF <svg version="1.1" width="$w" height="15" viewBox="0 0 $w 15" preserveAspectRatio="none" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" > <text x="2" y="11" style="font-size:12px">$out</text> </svg> EOF; exit;