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);
?>
Sat 01/06/02 at 19:08
Posts: 0
cjh wrote:
> Nimco wrote:
> 'a' - Open for writing only.
> 'a+' - Open for reading and writing.
>
>
> I thought 'w' was for writting??? Is there any difference???

'a' - places pointer at end of file.
'w' - places pointer at beginning of file + truncate to zero length

--------------------

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

i'm not sure about this, but try putting the count.dat file in htdocs. unless you have given everybody full write access using anonymous ftp, then i doubt its going to matter whether its accessible via a url. after all, they would have to guess at the file name - coz php is server-side, so they'll never see the file name.

then u can get rid of all the slashes (which are the wrong way round and are escaping the characters after them). then it will work....

btw, any reason ur variable is called $counfile rather than $countfile ???
Sat 01/06/02 at 17:12
Regular
"It goes so quickly"
Posts: 4,083
Nimco wrote:
> 'a' - Open for writing only.
> 'a+' - Open for reading and writing.


I thought 'w' was for writting??? Is there any difference???
Sat 01/06/02 at 16:17
Regular
"Eff, you see, kay?"
Posts: 14,156
Your slashes are the wrong way round for a start, that's going to screw it up.
Sat 01/06/02 at 14:52
Regular
"whoneedsatagline?"
Posts: 194
Whoa - now I'm seriously confused. I've been trying another way of doing this, and now a straightforward script like this:

$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?
Sat 01/06/02 at 14:18
Posts: 0
cjh wrote:
> doc693 wrote:
> $cfile = fopen("filename.dat", "a+");
>
> What is the + after the a for???

'a' - Open for writing only.
'a+' - Open for reading and writing.
Sat 01/06/02 at 11:13
Regular
"It goes so quickly"
Posts: 4,083
doc693 wrote:
> $cfile = fopen("filename.dat", "a+");

What is the + after the a for???
Sat 01/06/02 at 10:41
Regular
"whoneedsatagline?"
Posts: 194
Yes, I probably grabbed bits of the code from somewhere or other, ages ago. I guess the problem is contention for the file, and I'm going to have to try to get round it somehow. Not sure how a separate file woould help, but I'll work on it. I might go looking at Hotscripts.com to find another example.
Another problem is that flock() doesnt work at all with my webserver on Win98, so its impossible to test on my PC, before uploading.
Fri 31/05/02 at 00:09
Regular
"Eff, you see, kay?"
Posts: 14,156
I have deja-vu... Was this code ripped from anywhere? Freeola maybe? Parts of?

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.
Thu 30/05/02 at 23:44
Regular
"whoneedsatagline?"
Posts: 194
Hey, at least I got a reply.

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.
Thu 30/05/02 at 21:36
Regular
"Eff, you see, kay?"
Posts: 14,156
Why the hell are you using flock at all? Oh, and

if (isset($visitor_ip)) $addone = 0;

isn't valid syntax.

Freeola & GetDotted are rated 5 Stars

Check out some of our customer reviews below:

Thank you very much for your help!
Top service for free - excellent - thank you very much for your help.
Just a quick note to say thanks for a very good service ... in fact excellent service..
I am very happy with your customer service and speed and quality of my broadband connection .. keep up the good work . and a good new year to all of you at freeola.
Matthew Bradley

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.