GetDotted Domains

Viewing Thread:
"Hit counter problem (php)"

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.

Thu 30/05/02 at 20:13
Regular
Posts: 787
Hi,

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);
?>
There have been no replies to this thread yet.
Thu 30/05/02 at 20:13
Regular
"whoneedsatagline?"
Posts: 194
Hi,

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);
?>

Freeola & GetDotted are rated 5 Stars

Check out some of our customer reviews below:

Continue this excellent work...
Brilliant! As usual the careful and intuitive production that Freeola puts into everything it sets out to do, I am delighted.
First Class!
I feel that your service on this occasion was absolutely first class - a model of excellence. After this, I hope to stay with Freeola for a long time!

View More Reviews

Need some help? Give us a call on 01376 55 60 60

Go to Support Centre
Feedback Close Feedback

It appears you are using an old browser, as such, some parts of the Freeola and Getdotted site will not work as intended. Using the latest version of your browser, or another browser such as Google Chrome, Mozilla Firefox, or Opera will provide a better, safer browsing experience for you.