Re: [PHP] Website Hit counter and Logger

2007-04-20 Thread Daniel Brown

   Here's an idea for testing purposes to determine why it's counting low,
too

   Have your PHP script write to two files --- one that contains the number
as it is now, and one that writes the number and a timestamp on a new line
each time it's accessed.  This way, you can scan down the list to see where
the error occurs, and then cross-check it with your Apache logs at the time
indicated by the timestamp.


On 4/20/07, Amos Vryhof <[EMAIL PROTECTED]> wrote:


Thanks Daniel, I will switch that.  Like I said, I copied it from
someone's (obviously erroneous) example and didn't want to "mess it
up"... I usually use variable++ for increments.

Daniel Brown wrote:
>One not, Amos, is that instead of using $count += 1, you should just
do
> $count++.  That will automatically increment the number.
>
> On 4/20/07, Amos Vryhof <[EMAIL PROTECTED]> wrote:
>>
>> I build websites for a lot of people, and many of them want statistics
>> and hit counters for their websites.
>>
>> 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.
>>
>> 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.
>>
>> 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);
>> fputs($fp, $count);
>> flock($fp, 3);
>> fclose($fp);
>> } else {
>> echo "Can't find file, check '\$logfile'";
>> }
>>
>> which I found in an example somewhere (maybe on the example code page?)
>> but that seems to lose track as well.
>>
>> The server is hosted by AIT, running Apache 1.3.34.
>>
>> --
>> 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





--
Daniel P. Brown
[office] (570-) 587-7080 Ext. 272
[mobile] (570-) 766-8107


Re: [PHP] Website Hit counter and Logger

2007-04-20 Thread Amos Vryhof
Thanks Daniel, I will switch that.  Like I said, I copied it from 
someone's (obviously erroneous) example and didn't want to "mess it 
up"... I usually use variable++ for increments.


Daniel Brown wrote:

   One not, Amos, is that instead of using $count += 1, you should just do
$count++.  That will automatically increment the number.

On 4/20/07, Amos Vryhof <[EMAIL PROTECTED]> wrote:


I build websites for a lot of people, and many of them want statistics
and hit counters for their websites.

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.

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.

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);
fputs($fp, $count);
flock($fp, 3);
fclose($fp);
} else {
echo "Can't find file, check '\$logfile'";
}

which I found in an example somewhere (maybe on the example code page?)
but that seems to lose track as well.

The server is hosted by AIT, running Apache 1.3.34.

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



Re: [PHP] Website Hit counter and Logger

2007-04-20 Thread Amos Vryhof

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'";
}

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



Re: [PHP] Website Hit counter and Logger

2007-04-20 Thread Richard Lynch
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'";
>   }
>
> 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



[PHP] Website Hit counter and Logger

2007-04-20 Thread Amos Vryhof
I build websites for a lot of people, and many of them want statistics 
and hit counters for their websites.


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.


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.


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);
fputs($fp, $count);
flock($fp, 3);
fclose($fp);
} else {
echo "Can't find file, check '\$logfile'";
}

which I found in an example somewhere (maybe on the example code page?) 
but that seems to lose track as well.


The server is hosted by AIT, running Apache 1.3.34.

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



Re: [PHP] Website Hit counter and Logger

2007-04-20 Thread Daniel Brown

   One not, Amos, is that instead of using $count += 1, you should just do
$count++.  That will automatically increment the number.

On 4/20/07, Amos Vryhof <[EMAIL PROTECTED]> wrote:


I build websites for a lot of people, and many of them want statistics
and hit counters for their websites.

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.

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.

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);
fputs($fp, $count);
flock($fp, 3);
fclose($fp);
} else {
echo "Can't find file, check '\$logfile'";
}

which I found in an example somewhere (maybe on the example code page?)
but that seems to lose track as well.

The server is hosted by AIT, running Apache 1.3.34.

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





--
Daniel P. Brown
[office] (570-) 587-7080 Ext. 272
[mobile] (570-) 766-8107