I am running a counter on my web site and it was up to 40 hits yesturday. This morning I check it and it says 1. Also the same IP address was written twice. The counter I created logs the IP so that if the same IP comes back the same day, the hit won't count.
Here is my code:
// Path to count.log
$logfile = "/home/counter/count.log";
// Path to "count" directory (with trailing slash '/')
$countpath = "/home/www/counter/";
$addr = $REMOTE_ADDR;
$date = date("m-d-Y");
$ipFile = "$countpath" . "$date" . ".log";
if (file_exists("$ipFile")) {
$match = false;
$i = 0;
$ip = file("$ipFile");
$num = count($ip);
while ($i <= $num) {
if (chop($ip[$i]) == $addr) {
$match = true;
break;
}
$i++;
}
if ($match == false) {
$addrfile = fopen("$ipFile",a);
flock($addrfile,2);
fwrite($addrfile, "$addr" . "\n");
flock($addrfile,3);
fclose($addrfile);
$countfile = fopen("$logfile",r);
flock($countfile,2);
$number = fgets($countfile, 50);
$number = chop($number);
$number += 1;
$countfile = fopen("$logfile",w);
if (!fwrite($countfile, $number)) {
echo "A script error has occured!";
}
flock($countfile,3);
fclose($countfile);
}
} else {
$yesterday = time() - 86400;
$yesterday = date("m-d-Y", $yesterday);
$yesterdaylog = "$countpath" . "$yesterday" . ".log";
if (file_exists("$yesterdaylog")) {
unlink($yesterdaylog);
}
$addrfile = fopen("$ipFile","w");
flock($addrfile,2);
fwrite($addrfile, "$addr" . "\n");
flock($addrfile,3);
fclose($addrfile);
$countfile = fopen("$logfile",r);
flock($countfile,2);
$number = fgets($countfile, 50);
$number = chop($number);
$number += 1;
$countfile = fopen("$logfile",w);
if (!fwrite($countfile, $number)) {
echo "A script error has occured!";
}
flock($countfile,3);
fclose($countfile);
}
Any ideas why it might be doing this?