3 +----------------------------------------------+
\r
5 | PHP apache log parser class |
\r
7 +----------------------------------------------+
\r
8 | Filename : apache-log-parser.php |
\r
9 | Created : 21-Sep-05 23:28 GMT |
\r
10 | Created By : Sam Clarke |
\r
11 | Email : admin@free-webmaster-help.com |
\r
14 +----------------------------------------------+
\r
16 http://www.phpclasses.org/browse/download/1/file/10984/name/apache-log-parser.php
\r
20 This program is free software; you can redistribute it and/or
\r
21 modify it under the terms of the GNU General Public License (GPL)
\r
22 as published by the Free Software Foundation; either version 2
\r
23 of the License, or (at your option) any later version.
\r
25 This program is distributed in the hope that it will be useful,
\r
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
28 GNU General Public License for more details.
\r
30 To read the license please visit http://www.gnu.org/copyleft/gpl.html
\r
34 class apache_log_parser
\r
37 var $bad_rows; // Number of bad rows
\r
38 var $fp; // File pointer
\r
40 function format_log_line($line)
\r
42 preg_match("/^(\S+) (\S+) (\S+) \[([^:]+):(\d+:\d+:\d+) ([^\]]+)\] \"(\S+) (.*?) (\S+)\" (\S+) (\S+) (\".*?\") (\".*?\")$/", $line, $matches); // pattern to format the line
\r
46 function format_line($line)
\r
48 $logs = $this->format_log_line($line); // format the line
\r
50 if (isset($logs[0])) // check that it formated OK
\r
52 $formated_log = array(); // make an array to store the lin info in
\r
53 $formated_log['ip'] = $logs[1];
\r
54 $formated_log['identity'] = $logs[2];
\r
55 $formated_log['user'] = $logs[2];
\r
56 $formated_log['date'] = $logs[4];
\r
57 $formated_log['time'] = $logs[5];
\r
58 $formated_log['timezone'] = $logs[6];
\r
59 $formated_log['method'] = $logs[7];
\r
60 $formated_log['path'] = $logs[8];
\r
61 $formated_log['protocal'] = $logs[9];
\r
62 $formated_log['status'] = $logs[10];
\r
63 $formated_log['bytes'] = $logs[11];
\r
64 $formated_log['referer'] = $logs[12];
\r
65 $formated_log['agent'] = $logs[13];
\r
66 return $formated_log; // return the array of info
\r
70 $this->badRows++; // if the row is not in the right format add it to the bad rows
\r
75 function open_log_file($file_name)
\r
77 $this->fp = fopen($file_name, 'r'); // open the file
\r
80 return false; // return false on fail
\r
82 return true; // return true on sucsess
\r
85 function close_log_file()
\r
87 return fclose($this->fp); // close the file
\r
90 // gets a line from the log file
\r
93 if (feof($this->fp))
\r
97 $line = fgets ($this->fp, 1024);
\r