...

View Full Version : Counter resetting for some reason



mistux
11-18-2006, 04:36 PM
I have several pages that all use the following counter code. The problem is that it seems to keep resetting back to zero (or some other very low number, I don't really know for sure). I have set it to several large numbers like 2 million and it works fine for a while then I go back and it shows something like 243 or whatever. It does not happen right away, sometimes it is daily, some weekly, others monthly.


$fp=fopen("sheep_counter.txt","r");

//Read the previous count
$count=fgets($fp,1024);

//close the file.
fclose($fp);

$fw=fopen("sheep_counter.txt","w");

//Increment the counter
$cnew=$count+1;

//write the counter back to the log file ie., sheep_counter.txt
$countnew=fputs($fw,$count+1);

//Display VISITOR NUMBER
echo "<br> <p align=center> You are the $cnew Visitor to This site";
fclose($fw);

mlseim
11-19-2006, 01:07 AM
For a 32-bit system, the maximum integer size is: 2147483647
So that's not your problem.

The next thing I'm thinking about is multiple access at the same time.
You might need to file-lock it so it can't be written by two people
at the same time .... see this: http://www.hudzilla.org/php/8_11_0.php

Finally, you could separate so each page uses it's own text file and
use a separate PHP script to analyze all of them together. That would
also allow you to check the counts on each of them separately.

mistux
11-19-2006, 02:52 AM
Sorry about the confussion, I have several pages that use the same code in their own individual pages, and DO write to seperate files.

mlseim
11-19-2006, 03:34 AM
It might be interesting to experiment.
Try flock (file locking) on one page, but not
the others. See of that one page works.

CFMaBiSmAd
11-19-2006, 04:02 AM
There is a brief period of time when one access will execute the fopen("sheep_counter.txt","w"), this will cause a zero length file to exist. If another visitor accesses at this time, he will read a zero length file (he might get a read error as the file would currently be open for writing.) In either case, the second visitor will unconditionally do his own fopen "w" and write a 1 into the file.

There needs to be some error checking and if there was a read error, skip writing to the file.

mistux
11-19-2006, 11:30 PM
So if I do some sort of If..Then to see if the file is currently locked, then what? Can I deley and then try again or just cancile the writting all together. If so, then my code isn't much use as a counter I guess.

What is the best way to keep track of how many time the page was hit?

mistux
11-19-2006, 11:53 PM
Would setting up some sort of deley then retry to see if it is locked or not work?

I mean, check to see if it is locked, if so, then deley and check again, repeat until it is open then do the write.

If so, what would the code be?

mlseim
11-20-2006, 04:43 PM
Isn't that the whole point of using flock?

http://www.w3schools.com/PHP/func_filesystem_flock.asp?output=print

Why not lock the file and do what you want.
Nobody else can write to it while it's locked.

mistux
11-21-2006, 03:15 AM
Isn't that the whole point of using flock?

http://www.w3schools.com/PHP/func_filesystem_flock.asp?output=print

Why not lock the file and do what you want.
Nobody else can write to it while it's locked.

Please correct me if I am wrong, but won't flcok cause the conflicting writes not to be written at all? Which is why I mentioned it not being a good counter then.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum