How to search an Apache log file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jay
Forum Newbie
Posts: 22
Joined: Fri Jul 12, 2002 8:36 am
Location: KUL, Malaysia

How to search an Apache log file?

Post by Jay »

Has anybody here written a simple PHP script to search their Apache access log file?

What did you use to read the file into your script? All my attempts previously 'timed-out' since these files are usually large.

Any suggestion(s) will be appreciated...

Thank you.
MeOnTheW3
Forum Commoner
Posts: 48
Joined: Wed Nov 13, 2002 3:28 pm
Location: Calgary, AB

Post by MeOnTheW3 »

For the time limit: use set_time_limit()

Code: Select all

<?php
set_time_limit(300) // 300 seconds: 5 minutes
?>
For the length of the files: use readline();

Code: Select all

<?php
while(readline($filePointer) != FALSE)
{
   Process line
   -  add to arrary, etc
   ...
}
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

while(readline($filePointer) != FALSE)
minor correction

Code: Select all

while($line = fgets($filePointer))
or (if there are blank lines within the file)

Code: Select all

while(!feof($filePointer))
{
   $line = fgets($filePointer);
...

http://www.php.net/manual/en/function.fgets.php
string fgets ( int fp [, int length])
Returns a string of up to length - 1 bytes read from the file pointed to by fp.
http://www.php.net/manual/en/function.readline.php
string readline ( [string prompt])
This function returns a single string from the user. You may specify a string with which to prompt the user.
http://www.php.net/manual/en/function.fopen.php
http://www.php.net/manual/en/function.feof.php
Jay
Forum Newbie
Posts: 22
Joined: Fri Jul 12, 2002 8:36 am
Location: KUL, Malaysia

Post by Jay »

I was trying to avoid that... setting the time limit. I am almost disappointed there seems to be no other way... or perhaps I am just ignorant of some other efficient method to get this done.

Any other ideas?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Do you have to parse the file file over and over again?
If not you might set the filepointer to another position.
http://www.php.net/manual/en/function.fseek.php
User avatar
BigE
Site Admin
Posts: 139
Joined: Fri Apr 19, 2002 9:49 am
Location: Missouri, USA
Contact:

Post by BigE »

I made an Apache log viewer one time. I don't know if it still works, but you can check it out at EvilWalrus.com http://www.evilwalrus.com/viewcode/377.php It can easily be modified to search the log file. Hope it helps.
Jay
Forum Newbie
Posts: 22
Joined: Fri Jul 12, 2002 8:36 am
Location: KUL, Malaysia

Post by Jay »

volka wrote:Do you have to parse the file file over and over again?
If not you might set the filepointer to another position.
http://www.php.net/manual/en/function.fseek.php
Volka, this is exactly what I had done! The script works okay (because I was desperate for the information I needed to mine off the logs) but isn't this like a bit retarded?

BigE, thanks for the code, it's similar to what I have done so far except that I wrote the searched lines to a file by using fseek(), header() and I used preg_match_all() to power the search...

I have been reading up on the 'experimental' Command Line Interface(CLI) on the manual but sadly, I couldn't figure it out... yet. Anybody familiar with this?

Thank you guys.
Post Reply