💡Statistics Plugin(轻量版) - 累积访问数、今日访问数、目前在线数,3个功能独立引用。JSON 文件存储。
📁目录结构
plugin/ # 插件目录
└── statistics/ # 站点统计插件目录
├── core.php # 核心读写逻辑
├── tracker.php # 访问记录(footer顶部引入一次)
├── stats_total.php # 读取总访问数
├── stats_today.php # 读取今日访问数
├── stats_online.php # 读取在线人数
└── data/ # 数据存储目录
├── .htaccess # 自动生成,禁止Web访问
├── stats.json # 统计数据(自动生成)
└── ips.json # IP记录(自动生成)
📦安装
第一步:修改 core.php 中的盐值
define('STATS_SALT', '改成你的随机字符串'); //盐值保护哈希
第二步:在 footer.php 中引入
<!-- ⚠️顶部引入,记录访问 -->
<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/plugin/statistics/tracker.php';
?>
<!-- 📊总访问数 -->
<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/plugin/statistics/stats_total.php';
echo $stats_total;
?>
<!-- 📈今日访问数 -->
<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/plugin/statistics/stats_today.php';
echo $stats_today;
?>
<!-- 👥当前在线数 -->
<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/plugin/statistics/stats_online.php';
echo $stats_online;
?>
🧩功能说明
| 变量 | 说明 |
|---|---|
$stats_total |
累积总访问次数,每次访问 +1 |
$stats_today |
今日访问人数,去重IP,每天凌晨自动重置 |
$stats_online |
在线人数,6小时内去重IP,每隔6小时由访客触发更新 |
⚠️注意事项
data/目录需要 PHP 可写权限- Nginx 用户需手动屏蔽 data 目录:
location ~* /plugin/statistics/data/ { deny all; }
👨💻本站页脚示例
<span title="<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/blog2-3/plugin/statistics/stats_today.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/blog2-3/plugin/statistics/stats_online.php';
echo "📈 今日访问:" . $stats_today . "\n📊 当前在线:" . $stats_online;
?>" style="cursor: help;">
〔 感恩 <?php require_once $_SERVER['DOCUMENT_ROOT'] . '/blog2-3/plugin/statistics/stats_total.php';
echo $stats_total; ?> 次相遇 〕
</span>