counter with IP block via files - PHP
<p>I tried to write a counter for visits of my page, which only counts each IP once per day.
I transcribed a found code to understand how it works and to implement some other features later.
Unfortunately my new code doesn't work.</p>

<p>Code I made from it:</p>

<pre><code>&lt;?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 -&gt; Reset
if($time &gt;= $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);
}
?&gt;
</code></pre>

<p>with the following questions:</p>

<ul>
<li><p>what's wrong with my code?</p></li>
<li><p>flock is used to manage the access to the files, but why shall I use different names for the same file?</p></li>
</ul>

<p>Thanks for your suggestions.</p>

<p><b>EDIT:</b></p>

<p>Sorry for being so unspecific in explaining the problem. After integrating a debugger, I changed "REMOTE_ADDR" to "$_SERVER['REMOTE_ADDR']" so I fixed 1 error. Now i got the following problems:</p>

<pre><code>Warning: fopen(time.txt): failed to open stream: No such file or directory in .../counter.php on line 15
</code></pre>

<p>But the file is in the same directory as counter.php - of course the fread and filesize failed too</p>

<pre><code>Warning: fopen(ipsafe.txt): failed to open stream: Permission denied in .../counter.php on line 20
</code></pre>

<p>The file doesn't exist yet, but I thought "fopen($ipPath, "w+");" creats it if it doesn't exist.</p>

<pre><code>Fatal error: Call to undefined function fb() in .../counter.php on line 26
</code></pre>

<p>Doesn't "fb($timeFile);" send the value to firePHP?</p>
