php代码|记录一个ip60秒访问次数(php记录访问次数及ip)
时间:2023-06-09 16:48:59 阅读:648
<?php
// 创建zshao-1.txt文件
$file = fopen("zshao-1.txt", "a+");
fclose($file);
// 记录来访ip和时间
$ip = $_SERVER['REMOTE_ADDR'];
$time = date('Y-m-d H:i:s');
$file = fopen("zshao-1.txt", "a+");
fwrite($file, $ip . ' ' . $time . "\n");
fclose($file);
// 检测当前来访的ip一分钟内访问了多少次
$file = fopen("zshao-1.txt", "r");
$count = 0;
while (!feof($file)) {
$line = fgets($file);
if (strpos($line, $ip) !== false && strtotime($time) - strtotime(substr($line, strpos($line, ' ') + 1)) <= 60) {
$count++;
}
}
fclose($file);
echo "当前IP一分钟内访问了" . $count . "次";
?>

网友评论