php实现文件存储网站访问统计代码
代码语言:php
所属分类:其他
代码描述:php实现文件存储网站访问统计代码,无需依赖数据库,采用json文件存储访问数据 ,每天的数据分开多文件存储。
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<?php
/**
* 网站访问统计系统 (PHP 5.6 单文件版)
* 包含:统计类 + 可视化页面 + ECharts 图表
* 使用方法:直接访问此文件即可查看统计面板
*/
// ==================== 配置区域 ====================
$STATS_CONFIG = array(
'data_dir' => __DIR__ . '/data',
'only_get' => true,
'exclude_ajax' => true,
'exclude_static' => true,
'exclude_paths' => array('/admin/', '/api/', '/login', '/stats')
);
// ==================== 统计类 ====================
class VisitStats {
private $dataDir;
private $cacheDir;
private $config = array(
'only_get' => false,
'exclude_ajax' => false,
'exclude_static' => true,
'exclude_paths' => array(),
'include_paths' => array()
);
public function __construct($baseDir = null, $config = array()) {
if ($baseDir === null) {
$baseDir = __DIR__ . '/data';
}
$this->dataDir = $baseDir . '/stats';
$this->cacheDir = $baseDir . '/cache';
if (!empty($config) && is_array($config)) {
$this->config = array_merge($this->config, $config);
}
$this->ensureDir($this->dataDir);
$this->ensureDir($this->cacheDir);
}
private function ensureDir($dir) {
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
}
public function recordVisit($userId = null, $pageUrl = null) {
if (!$this->shouldRecord()) {
return false;
}
$ip = $this->getClientIP();
$date = date('Y-m-d');
$timestamp = time();
$hour = (int)date('H');
$userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$requestUri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
$method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
$data = array(
'ip' => $ip,
'timestamp' => $timestamp,
'hour' => $hour,
'method' => $method,
'user_agent' => $userAgent,
'referer' => $referer,
'page' => $pageUrl !== null ? $pageUrl : $requestUri,
'user_id' => $userId,
'bytes' => $this->estimateResponseSize(),
'is_ajax' => $this->isAjaxRequest() ? 1 : 0
);
$file = $this->dataDir . '/' . $date . '.json';
$this->appendToFile($file, $data);
$this->updateCache($date);
return true;
}
private function shouldRecord() {
// 统计页面本身不记录
$uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
if (strpos($uri, '/stats') !== false || strpos($uri, 'stats.php') !== false) {
return false;
}
$method = isset($_SERVER['REQUEST_METHOD']) ? strtoupper($_SERVER['REQUEST_METHOD']) : 'GET';
if ($this->config['only_get'] && $method !== 'GET') {
return false;
}
if ($this->config['exclude_ajax'] && $this->isAjaxRequest()) {
return false;
}
if ($this->config['exclude_static'] && $this->isStaticResource($uri)) {
return false;
}
if (!empty($this->config['exclude_paths'])) {
foreach ($this->config['exclude_paths'] as $path) {
if (strpos($uri, $path) !== false) {
return false;
}
}
}
if (!empty($this->config['include_paths'])) {
$matched = false;
foreach ($this->config['include_paths'] as $path) {
if (strpos($uri, $path) !== false) {
$matched = true;
break;
}
}
if (!$matched) {
return false;
}
}
return true;
}
private function isAjaxRequest() {
if (isse.........完整代码请登录后点击上方下载按钮下载查看















网友评论0