The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.
I have a home-made php script that counts visits to my home page. It uses a simple text file to keep track. Problem is, twice recently, the counter has reset to zero, and started to build up again from there.
The counter script (see below) opens the text file, and uses flock() to try to lock the file. Then it adds 1, truncates the file to length 0, writes the counter and closes the file.
It also checks for the presence of a 1 hr cookie, so that it doesnt count returns to the page by the same person within that hour.
So ... why do I get reset to zero? Either someone's found out how to hack the file, or its to do with more than one user visiting at the same time, and the locking isnt working.
Any ideas, please?
$addone=1;
if (isset($visitor_ip)) $addone = 0;
$cfile = fopen("filename.dat", "a+");
$locked = flock($cfile,LOCK_EX);
rewind($cfile);
$vcounter = fgets($cfile, 255);
if (empty($vcounter)) {
$vcounter = 0;
$addone = 1;
}
$vcounter+=$addone;
if ($addone==1) {
setcookie ("visitor_ip", "addedone", time()+3600);
ftruncate($cfile,0);
fputs($cfile, $vcounter);
}
fclose($cfile);
?>
I have a home-made php script that counts visits to my home page. It uses a simple text file to keep track. Problem is, twice recently, the counter has reset to zero, and started to build up again from there.
The counter script (see below) opens the text file, and uses flock() to try to lock the file. Then it adds 1, truncates the file to length 0, writes the counter and closes the file.
It also checks for the presence of a 1 hr cookie, so that it doesnt count returns to the page by the same person within that hour.
So ... why do I get reset to zero? Either someone's found out how to hack the file, or its to do with more than one user visiting at the same time, and the locking isnt working.
Any ideas, please?
$addone=1;
if (isset($visitor_ip)) $addone = 0;
$cfile = fopen("filename.dat", "a+");
$locked = flock($cfile,LOCK_EX);
rewind($cfile);
$vcounter = fgets($cfile, 255);
if (empty($vcounter)) {
$vcounter = 0;
$addone = 1;
}
$vcounter+=$addone;
if ($addone==1) {
setcookie ("visitor_ip", "addedone", time()+3600);
ftruncate($cfile,0);
fputs($cfile, $vcounter);
}
fclose($cfile);
?>