Parsing raw apache logs
I need some php code for parsing raw apache logs. In particular, I want the number of times mode=search and the term used for searching. Here is an example:
207.46.195.228 - - 开发者_开发百科[30/Apr/2010:03:24:26 -0700] "GET /index.php?mode=search&term=AE1008787E0174 HTTP/1.1" 200 13047 "-" "msnbot/2.0b (+http://search.msn.com/msnbot.htm)"
212.81.200.167 - - [30/Apr/2010:04:21:43 -0700] "GET /index.php?mode=search&term=WH2002D-YYH HTTP/1.1" 200 12079 "http://www.mysite.com/SearchGBY.php?page=81" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB6.4; .NET CLR 1.1.4322; .NET CLR 2.0.50727; WinuE v6; InfoPath.2; WinuE v6)"
212.81.200.167 - - [30/Apr/2010:04:21:44 -0700] "GET /file_uploads/banners/banner.swf HTTP/1.1" 200 50487 "-" "contype"
66.249.68.168 - - [30/Apr/2010:04:21:45 -0700] "GET /index.php?mode=search&term=WH2002D-YYH HTTP/1.1" 200 12079 "-" "Mediapartners-Google"
I recently wrote a very crude parser for this:
$ignore = array('css', 'png', 'gif', 'jpg', 'jpeg', 'js', 'ico');
$f = fopen('access_log', "r");
if(!$f) die("Failed to open log for reading.");
while (!feof($f)) {
$buff = fgets($f, 4096);
$parts = explode(' ', $buff);
if(in_array(end(explode('.', $parts[6])), $ignore)) continue;
$domain = trim(end($parts));
// http method
$http_method = substr($parts[5], 1);
if($http_method != 'GET' && $http_method != 'POST') continue;
// parse out the date
list($d, $m, $y) = explode('/', substr($parts[3], 1));
$y = substr($y, 0, 4);
$time = strtotime("{$d} {$m} {$y}");
print "{$time} {$parts[0]} {$http_method} {$parts[6]} $domain\n";
}
$parts[6] should contain the part you're interested in (the resource that was accessed). This should get you on your way...
As easy as using regular expressions: http://php.net/manual/en/book.regex.php
精彩评论