Hey guys, I need to read through my apache error_log file and filter out only what I'm looking for. (specific IP addresses)
Below is the normal format in the file:
192.168.2.1 - - [20/Mar/2009:21:03:38 -0400] "GET /index.php HTTP/1.1" 200 58
192.168.2.3 - - [20/Mar/2009:21:03:38 -0400] "GET /index.php HTTP/1.1" 200 58
192.168.2.3 - - [20/Mar/2009:21:03:38 -0400] "GET /index.php HTTP/1.1" 200 58
192.168.2.5 - - [20/Mar/2009:21:03:38 -0400] "GET /index.php HTTP/1.1" 200 58
192.168.2.5 - - [20/Mar/2009:21:03:38 -0400] "GET /index.php HTTP/1.1" 200 58
192.168.2.1 - - [20/Mar/2009:21:03:38 -0400] "GET /index.php HTTP/1.1" 200 58
Here's my code but it doesn't seem to work right?
What I'm trying to do is, as I read through the file, output anything that matches in the array....
Code:
<?php
$bad_ip_address = array('192.168.2.1','192.168.2.3');
$fh = fopen("error_log",'r');
while (!feof($fh)) {
$buffer = fgets($fh, 4096);
if(in_array("$bad_ip_address", $buffer)) {
echo $buffer;
}
}
fclose($fh);
?>
So based on the array, my output should be:
192.168.2.1 - - [20/Mar/2009:21:03:38 -0400] "GET /index.php HTTP/1.1" 200 58
192.168.2.3 - - [20/Mar/2009:21:03:38 -0400] "GET /index.php HTTP/1.1" 200 58
192.168.2.3 - - [20/Mar/2009:21:03:38 -0400] "GET /index.php HTTP/1.1" 200 58
192.168.2.1 - - [20/Mar/2009:21:03:38 -0400] "GET /index.php HTTP/1.1" 200 58