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);
?>
if (isset($visitor_ip)) $addone = 0;
isn't valid syntax.
I was using flock because I thought it would help ... i.e. maybe prevent more than one client from trying to read/write the file at the same time. You obviously think not, Turbo. But why then would the counter get set back to zero?
And re that syntax, it is valid ;), even if not 100% structured. But I'm not after an argument on that - it wasnt the question.
Anyway, I can see the use of flock, but I would say that's what's screwing it up - what I think's happening is this:
You're flocking the file while it's being processed. If another client opens it at this point, it won't, and $vcounter is empty. That means that your $vcounter is set to zero, and then added to. Offset logic means that by the time the script writes it again, the file is unlocked and the new value gets written - 1. I'd say you need to get round this, maybe with a lockfile that increments while the file is being written to by other clients, and decremented when it's finished. This means you can justa dd on the value of the lock file, which should absorb the overwrite.
Another problem is that flock() doesnt work at all with my webserver on Win98, so its impossible to test on my PC, before uploading.
> $cfile = fopen("filename.dat", "a+");
What is the + after the a for???
$counfile = "..\webfiles\count.dat";
$cf = fopen($counfile,"r");
$vcounter = fgets($cf,250);
fclose ($cf);
echo "$counfile : Counter is $vcounter";
?>
... returns $vcounter = 10, even though count.dat contains the value 11207.
This "webfiles" directory of mine is at the same level as "htdocs", rather than within it - so that the files can't be accessed via a url. I've never had a problem with this before, so has something changed?
Is there some caching going on somewhere that I'm not aware of? Otherwise, where on earth is that "10" coming from?
> 'a' - Open for writing only.
> 'a+' - Open for reading and writing.
I thought 'w' was for writting??? Is there any difference???