我试图为我的页面的访问编写一个计数器,它每天只计算每个 IP 一次。我转录了一个找到的代码来了解它是如何工作的,并在以后实现一些其他功能。不幸的是,我的新代码不起作用。
我从中制作的代码:
<?php
$timeReset = 86400; //24h
$ipPath = "ipsafe.txt";
$countPath = "counts.txt";
$timePath = "time.txt";
$ip = $REMOTE_ADDR;
$time = time();
$resetTime = $time+$timeReset;
$timeFile = fopen($timePath, "r+");
$timeData = fread($timeFile, filesize($timePath));
//if resetTime is passed -> Reset
if($time >= $timeData) {
//clear ipSafe
$ipFile1 = fopen($ipPath, "w+");
fclose($ipFile);
//set new resetTime
rewind($timeData);
fwrite ($timeData, $resetTime);
}
fclose($timeFile);
//creat IP-List
$ipData = file($ipPath);
while (list ($line_num, $line) = each ($ipData)) {
$digits = explode(",",$line);
}
//if IP was not saved in last timeframe
if(!in_array($ip, $digits))
{
//write new IP
$ipFile2=fopen($ipPath, "a+");
@flock($ipFile1,2);
fwrite($ipFile2, "$ip".",");
@flock($ipFile1,3);
fclose($ipFile2);
//increment counter
$countFile = fopen($countPath,"r+");
$countData = fread($countFile, filesize($countPath);
rewind($countFile);
$countData++;
fwrite($countFile, $countData);
fclose($countFile);
}
?>
有以下问题:
我的代码有什么问题?
flock 用于管理对文件的访问,但是为什么我要为同一个文件使用不同的名称呢?
感谢您的建议。
编辑:
很抱歉在解释问题时如此不具体。集成调试器后,我将“REMOTE_ADDR”更改为“$_SERVER['REMOTE_ADDR']”,因此修复了 1 个错误。现在我遇到了以下问题:
Warning: fopen(time.txt): failed to open stream: No such file or directory in .../counter.php on line 15
但是该文件与 counter.php 位于同一目录中-当然 fread 和 filesize 也失败了
Warning: fopen(ipsafe.txt): failed to open stream: Permission denied in .../counter.php on line 20
该文件尚不存在,但我认为“fopen($ipPath, "w+");" 如果它不存在则创建它。
Fatal error: Call to undefined function fb() in .../counter.php on line 26
不是“fb($timeFile);” 将值发送到 firePHP?