On Fri, April 20, 2007 11:06 am, Amos Vryhof wrote:
> I build websites for a lot of people, and many of them want statistics
> and hit counters for their websites.

Webalizer
Analog

You can probably find binaries for both, or download logfiles and run
them offline or...

> Since not all of them are on a dedicated or virtual host, I cobbled
> together a script that builds log files, and displays a graphical hit
> counter, and another script that runs Webalizer and goes to the
> report.

Wait...

You CREATE logfiles from PHP as if they were Apache logfiles, and run
them through Webalizer?...

Surely the host provides access to log files, no?...

> My dilemma is that the hit counter portion of the script works at
> first,
> then at some point loses track, and displays a number that is way off
> from the actual number.

Way off low, or way off high?

> I was originally using code that read the number from a text file,
> added
> 1 to it, then wrote it back using file_get/put_contents.
>
> Since then I have changed to
>
>       if (file_exists($logfile)) {
>               $fp = fopen("$logfile", "r+");
>               flock($fp, 1);
>               $count = fgets($fp, 4096);
>               $count += 1;
>               fseek($fp,0);

Seems to me you need another flock($fp, 2) here to get exclusive write
access to the file...

Otherwise, you haven't avoided the race condition at all, you've just
added a bunch of flock calls that don't do much of anything...

>               fputs($fp, $count);
>               flock($fp, 3);
>               fclose($fp);
>       } else {
>               echo "Can't find file, check '\$logfile'<BR>";
>       }
>
> which I found in an example somewhere (maybe on the example code
> page?)
> but that seems to lose track as well.

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

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

Reply via email to