// 连接数据库
$file = 'data.txt';
$handle = fopen($file, 'a+');
$ip = $_SERVER['REMOTE_ADDR'];

// 获取历史数据
$history_data = file($file);
$history_count = count($history_data);
$history_ips = array();
foreach ($history_data as $history_ip) {
    array_push($history_ips, $history_ip);
}
$unique_history_ips = array_unique($history_ips);
$unique_history_count = count($unique_history_ips);

// 获取今日数据
$today_data = array();
$today_ips = array();
while (!feof($handle)) {
    $line = fgets($handle);
    if (strpos($line, date('Y-m-d')) !== false) {
        array_push($today_data, $line);
        array_push($today_ips, explode(' ', $line)[0]);
    }
}
rewind($handle);
$unique_today_ips = array_unique($today_ips);
$unique_today_count = count($unique_today_ips);

// 获取访问次数
$count = 0;
while (!feof($handle)) {
    $line = fgets($handle);
    if (strpos($line, $ip) !== false) {
        $count++;
    }
}

// 写入今日数据
if (!in_array('$ip ' . date('Y-m-d') . '\n', $today_data)) {
    fwrite($handle, '$ip ' . date('Y-m-d') . '\n');
}

// 关闭数据库连接
fclose($handle);

// 输出统计结果
echo '访问者IP: $ip <br>';
echo '独立IP数(历史): $unique_history_count <br>';
echo '独立IP数(今日): $unique_today_count <br>';
echo '历史数据: $history_count <br>';
echo '今日数据: ' . count($today_data) . ' <br>';
echo '访问次数: $count';

?>
PHP 统计页面:访问者IP、独立IP、历史数据、今日数据、访问次数

原文地址: https://www.cveoy.top/t/topic/mOe7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录