Page 1 of 1
How to search an Apache log file?
Posted: Fri Nov 29, 2002 1:14 pm
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.
Posted: Fri Nov 29, 2002 4:10 pm
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
...
}
?>
Posted: Fri Nov 29, 2002 4:34 pm
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.phpstring 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.phpstring 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
Posted: Fri Nov 29, 2002 5:45 pm
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?
Posted: Fri Nov 29, 2002 8:29 pm
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
Posted: Sat Nov 30, 2002 2:41 am
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.
Posted: Sat Nov 30, 2002 4:41 am
by Jay
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.