The difference is that you are getting a string from the file and not
casting it to an integer.  You could also have fixed it by doing:

  $counter = (int) fread(...);

Note however that you have a nasty race condition in your script.  If you
get concurrent hits they will all read the same counter value out of your
counter file, increment it, and write the same value back.  So those hits
will not be counted.

An easy way to get around this is to only open your file in append mode
and add a single char to the file for every hit.  filesize() on the file
then gives you the count.  The advantage here is that an append to a file
is guranteed to be atomic and you won't have that race condition between
reading and writing the file.

The other way around it is to lock your file, but locking files in a web
app is a really bad idea.

And finally, to really make a robust counter, use a database and an atomic
count=count+1 query.

-Rasmus

On Fri, 4 Jul 2003, Kyle Babich wrote:

> No, because if i did that the counter would increment no matter what the
> IP address is.  The way I have it the counter will only increment if it
> is a new ip address to the site.  I just fixed it by switching
> $current++; to $current += 1;  Apparently there is some small difference
> between the two.
>
> On Sat, 5 Jul 2003 00:42:59 +1000, "Tom Rogers" <[EMAIL PROTECTED]>
> said:
> > Hi,
> >
> > Saturday, July 5, 2003, 12:33:25 AM, you wrote:
> > KB> Why does this not work?  It is just a simple hit counter (hence the
> > KB> snarls, hissing, and growling).  It logs the ips address but does not
> > KB> increment $current or log it.  I do have counter.txt and ips.txt
> > chmod'd
> > KB> to 777.  Ips.txt starts blank and counter.txt starts with just a 0 in
> > it.
> >
> > KB> <?php $counter = fopen('counter.txt', 'r');
> > KB> $current = fread($counter, filesize('counter.txt'));
> > KB> fclose($counter);
> >
> > KB> $ip = getenv('REMOTE_ADDR');
> > KB> $ipCheck = file('ips.txt');
> > KB> if (!in_array($ip, $ipCheck)) {
> > KB>     $ipAdd = fopen('ips.txt', 'a');
> > KB>     fwrite($ipAdd, "\n$ip");
> > KB>     fclose($ipAdd);
> >
> > KB>     $current++;
> > KB>     $counter = fopen('counter.txt', 'w');
> > KB>     fwrite($counter, $current);
> > KB>     fclose($counter);
> > KB> }
> >
> > print $current; ?>>
> > KB> --
> > KB> Kyle
> >
> >
> > maybe you need to end the if() statement before incrementing the
> > counter.
> > $ip = getenv('REMOTE_ADDR');
> > $ipCheck = file('ips.txt');
> > if (!in_array($ip, $ipCheck)) {
> >     $ipAdd = fopen('ips.txt', 'a');
> >     fwrite($ipAdd, "\n$ip");
> >     fclose($ipAdd);
> > }
> > $current++;
> > $counter = fopen('counter.txt', 'w');
> > fwrite($counter, $current);
> > fclose($counter);
> > print $current; ?>
> >
> > --
> > regards,
> > Tom
> >
> >
> --
> Kyle
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to