Log File - Counting "Hits"

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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:

$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


Re: Log File - Counting "Hits"

Quoted text here. Click to load it

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"


Re: Log File - Counting "Hits"

Ben Holness wrote:

Quoted text here. Click to load it

Thanks. I used the grep command.

Thanks for all your help.


Site Timeline