Re: Website Hit counter and Logger

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Richard Lynch wrote:
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

Webalizer works fine, and the output is good (though I doubt it's as accurate since it's tied into the hit counter). That part of the script is fully functional.


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

Yes, the host provides access to log files for domains and virtual hosts. The logs I create are for specific subdirectories where the sites reside.

Believe me, if I was in charge of how this was run, each customer would have a virtual host of their own and creating the logs wouldn't need to happen. As it is, I have to maintain two versions of the script. One for the site-in-a-directory situation, and the other for virtual hosts that uses server logs.


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?

Way off low. I assume something causing the number to be reset to zero at some point.


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

Thanks, I'll give that a try.

I had a hunch that it might have been happening when two people visit at once and the server just gives up on who gets write access to the file, but I haven't had much time to familiarize myself with file locking.


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.


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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux