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.
How to search an Apache log file?
Moderator: General Moderators
For the time limit: use set_time_limit()
For the length of the files: use readline();
Code: Select all
<?php
set_time_limit(300) // 300 seconds: 5 minutes
?>Code: Select all
<?php
while(readline($filePointer) != FALSE)
{
Process line
- add to arrary, etc
...
}
?>minor correctionwhile(readline($filePointer) != FALSE)
Code: Select all
while($line = fgets($filePointer))Code: Select all
while(!feof($filePointer))
{
$line = fgets($filePointer);
...http://www.php.net/manual/en/function.fgets.php
http://www.php.net/manual/en/function.readline.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.fopen.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.feof.php
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
If not you might set the filepointer to another position.
http://www.php.net/manual/en/function.fseek.php
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.
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?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
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.