无数据库纯文件版php访问统计代码

Captain 网络杂类 2021-06-29

⚪今日访问量
⚪总访问量
⚪在线人数
⚪独立访客统计
⚪防刷新机制(同一 IP 5 分钟内只算 1 次,可以按需酌情修改)

使用方法
新建文件夹,将代码保存为 index.php
直接运行(PHP 环境:PHP 5.6+ 均可)
运行后会自动生成两个文件:
count.txt:存储总访问量、今日访问量、日期
ip.txt:存储访客 IP 和访问时间

功能说明
✅ 防刷新:同一 IP 5 分钟内多次刷新只算 1 次
✅ 自动清零:每天 0 点自动重置今日访问量
✅ 在线人数:统计 5 分钟内活跃的访客
✅ 独立访客:基于 IP 去重统计
✅ 纯文件存储:无需数据库、无需安装扩展
✅ 美观界面:自带样式,直接可用

安全说明
代码对 IP、时间做了安全过滤
仅读写文本文件,无 SQL 注入风险
无敏感操作,服务器环境可直接使用

完整代码:

<?php
// ======================================
// PHP 页面访问统计 - 纯文件版
// ======================================

// 配置项
$ip = $_SERVER['REMOTE_ADDR'];          // 获取访客IP
$time = time();                        // 当前时间戳
$expire = 300;                          // IP有效期 5分钟 = 300秒
$online_expire = 300;                   // 在线超时时间 5分钟
$data_file = 'count.txt';              // 统计数据存储文件
$ip_file = 'ip.txt';                    // 访客IP记录文件

// --------------------
// 初始化统计文件
// --------------------
if (!file_exists($data_file)) {
    // 格式:总访问量|今日访问量|今日日期
    file_put_contents($data_file, "0|0|".date('Y-m-d'));
}
if (!file_exists($ip_file)) {
    file_put_contents($ip_file, '');
}

// --------------------
// 读取统计数据
// --------------------
$data = explode('|', file_get_contents($data_file));
$total = intval($data[0]);      // 总访问量
$today = intval($data[1]);      // 今日访问量
$today_date = $data[2];         // 今日日期

// --------------------
// 日期切换:新的一天重置今日统计
// --------------------
if ($today_date != date('Y-m-d')) {
    $today = 0;
    $today_date = date('Y-m-d');
}

// --------------------
// 读取IP记录
// --------------------
$ip_list = file($ip_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$ip_list = is_array($ip_list) ? $ip_list : [];
$new_ip_list = [];
$is_visit = false;

// --------------------
// 清理过期IP + 判断是否新访客
// --------------------
foreach ($ip_list as $line) {
    $temp = explode('|', $line);
    if (count($temp) != 2) continue;
    
    $old_ip = $temp[0];
    $old_time = intval($temp[1]);
    
    // 保留未过期的IP记录
    if ($old_time > $time - $expire) {
        $new_ip_list[] = $line;
        // 如果当前IP已存在,说明5分钟内访问过,不重复统计
        if ($old_ip == $ip) {
            $is_visit = true;
        }
    }
}

// --------------------
// 新访客:增加访问量
// --------------------
if (!$is_visit) {
    $total++;
    $today++;
    $new_ip_list[] = $ip.'|'.$time;
}

// --------------------
// 保存数据
// --------------------
file_put_contents($data_file, $total.'|'.$today.'|'.$today_date);
file_put_contents($ip_file, implode("\n", $new_ip_list));

// --------------------
// 统计在线人数
// --------------------
$online = 0;
foreach ($new_ip_list as $line) {
    $temp = explode('|', $line);
    $old_time = intval($temp[1]);
    if ($old_time > $time - $online_expire) {
        $online++;
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>PHP访问统计</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; font-family: "Microsoft YaHei"; }
        body { background: #f5f5f5; padding: 30px; }
        .count-box { 
            max-width: 500px; 
            margin: 0 auto; 
            background: #fff; 
            padding: 30px; 
            border-radius: 10px; 
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        .count-box h2 { 
            text-align: center; 
            color: #333; 
            margin-bottom: 25px;
            padding-bottom: 15px;
            border-bottom: 1px solid #eee;
        }
        .count-item { 
            display: flex; 
            justify-content: space-between; 
            padding: 12px 0; 
            border-bottom: 1px dashed #eee;
            font-size: 16px;
        }
        .count-item:last-child { border-bottom: none; }
        .label { color: #666; }
        .num { color: #007bff; font-weight: bold; font-size: 18px; }
    </style>
</head>
<body>
    <div class="count-box">
        <h2>📊 网站访问统计</h2>
        
        <div class="count-item">
            <span class="label">👥 当前在线人数:</span>
            <span class="num"><?php echo $online; ?></span>
        </div>
        
        <div class="count-item">
            <span class="label">📅 今日访问量:</span>
            <span class="num"><?php echo $today; ?></span>
        </div>
        
        <div class="count-item">
            <span class="label">🌐 总访问量:</span>
            <span class="num"><?php echo $total; ?></span>
        </div>
        
        <div class="count-item">
            <span class="label">🕒 统计时间:</span>
            <span class="num"><?php echo date('Y-m-d H:i:s'); ?></span>
        </div>
    </div>
</body>
</html>
PREV:Linux向文件尾部添加多行内容
NEXT:TCP和UDP核心区别,快速搞懂这两大协议!