Connecting Tech Pros Worldwide Forums | Help | Site Map

Log File - Counting "Hits"

StevePBurgess@gmail.com
Guest
 
Posts: n/a
#1: Jun 27 '06
Hi. I have a script that runs every night on my server that counts the
hits for my hosted RSS feeds and puts the information into a MySQL
table which then feeds the live(ish) statistics graphs on the site.

I am using:

$log=implode('',file("access_log"));
$count=substr_count($log,"GET /feed7.xml");

for example to count the number of times feed7.xml is requested.

As the popularity of my site increases, however, I have started to
notice these errors in the error log:

PHP Fatal error: Allowed memory size of 8388608 bytes exhausted (tried
to allocate 10802165 bytes) in

I know I could increase the memory settings in PHP - but is there a
smarter way of counting entries in the log file without trying to load
the entire thing into memory first?

Thanks, in anticipation

Steve


Ben Holness
Guest
 
Posts: n/a
#2: Jun 27 '06

re: Log File - Counting "Hits"


> I know I could increase the memory settings in PHP - but is there a[color=blue]
> smarter way of counting entries in the log file without trying to load
> the entire thing into memory first?[/color]

You could read the file in line by line and count as you go using fopen()
and fread() or fgets() .. See the PHP pages for how to use them.

Alternatively you could make a system call with "grep 'GET /feed7.xml' | wc -l"

Ben
StevePBurgess@gmail.com
Guest
 
Posts: n/a
#3: Jun 28 '06

re: Log File - Counting "Hits"



Ben Holness wrote:
[color=blue]
> You could read the file in line by line and count as you go using fopen()
> and fread() or fgets() .. See the PHP pages for how to use them.
>
> Alternatively you could make a system call with "grep 'GET /feed7.xml' | wc -l"
>
> Ben[/color]


Thanks. I used the grep command.

Thanks for all your help.

Steve

Closed Thread