View Single Post
Old 03-21-2009, 02:31 AM   #1 (permalink)
allworknoplay
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default Help search through file....

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
allworknoplay is offline  
Reply With Quote