eon201 Posted November 14, 2007 Share Posted November 14, 2007 Hi Im trying to compare a current value in a loop to all of the previous examples to assertain if a value is unique or not. Im attempting it like this... foreach($content as $temp=>$value) { $bits = explode(" ", $value); $log_clientIP[] = $bits[9]; $find = $log_clientIP; if (strpos($log_clientIP[], $find) == true){ echo 'This ip exists already'; //then do other stuff } if (strpos($log_clientIP[], $find) == false){ echo 'This ip does not currently exist'; // Then do other stuff } } This is all contained within a foreach loop which opens and reads lines from a txt file. I know that the problem is in $log_clientIP[] as I get the Fatal error: Cannot use [] for reading in.... What am I doing wrong? Should I even be using the strpos function for this? Quote Link to comment Share on other sites More sharing options...
aschk Posted November 14, 2007 Share Posted November 14, 2007 strpos takes a STRING as it's first argument. You have provided an array. It calculates the position (i.e. an integer) that the 1st string exists in the 2nd string. I suspect this ISN'T what you want. Quote Link to comment Share on other sites More sharing options...
eon201 Posted November 14, 2007 Author Share Posted November 14, 2007 Thanks. I'll remember that little gem. How would you suggest I go about my task now?? Quote Link to comment Share on other sites More sharing options...
eon201 Posted November 14, 2007 Author Share Posted November 14, 2007 Im thinking that i have to loop inside the original loop, and then compare the two like so... $bitspast = explode(" ", $value); $log_clientIPpast[] = $bitspast[9]; $log_requestpast[] = $bitspast[5]; foreach ($content as $key2=>$value2){ $bitspresent = explode(" ", $value2); $log_clientIPpresent[] = $bitspresent[9]; $log_requestpresent[] = $bitspresent[5]; $hour = substr($value, 11, 2); $hour = $hour *60; $minute = substr($value, 14, 2); $minutes = $minute + $hour; // These are the minutes for this line echo "$bitspresent[9] My IP <br/>"; echo "$minutes - The time (as minutes)<br/>"; //echo the minutes for this day. echo "$bitspresent[1] The time<br/>"; if (array_search('$bitspresent[9]', $bitspast) == true){ echo 'This is an original ip<br/><br/>'; } else{echo 'I didnt find the ip - which MUST be a lie<br/><br/>';} } //end of second foreach statement }//end of if strip statement }//end of first foreach loop But this just gives me the output of... Notice: Undefined offset: 9 in D:\home\Default\planetcruise-svr.co.uk\htdocs\pcm\logreader5.php on line 105 Notice: Undefined offset: 5 in D:\home\Default\planetcruise-svr.co.uk\htdocs\pcm\logreader5.php on line 107 Notice: Undefined index: 9 in D:\home\Default\planetcruise-svr.co.uk\htdocs\pcm\logreader5.php on line 114 My IP 1410 - The time (as minutes) Notice: Undefined index: 1 in D:\home\Default\planetcruise-svr.co.uk\htdocs\pcm\logreader5.php on line 116 The time I didnt find the ip - which MUST be a lie 217.212.224.162 My IP 1410 - The time (as minutes) 23:58:22 The time I didnt find the ip - which MUST be a lie 170.194.32.52 My IP 1410 - The time (as minutes) 23:58:22 The time I didnt find the ip - which MUST be a lie 204.10.146.100 My IP 1410 - The time (as minutes) 23:57:54 The time I didnt find the ip - which MUST be a lie Has anyone else ever tried to do a comparison like this before??? If so please lend a hand, some advice/help would be great! Quote Link to comment Share on other sites More sharing options...
aschk Posted November 14, 2007 Share Posted November 14, 2007 Can you post ALL of the code + the input data you are giving the function/procedure Quote Link to comment Share on other sites More sharing options...
eon201 Posted November 14, 2007 Author Share Posted November 14, 2007 Ok. Here is the full code... <?php set_time_limit(30); // Run this script every 15 minutes! $filename = 'logs/'. 'ex'. date('ymd'). '.log'; // Filename Checker // D:\home\Planet Cruise Sites\planetcruise.co.uk\logs\W3SVC10019\ if (file_exists($filename)) // Does the corresponding filename exist - if yes then { $fp = fopen($filename, "r"); //Open the server log $content = fread($fp, filesize($filename)); // Read the server log $content = explode("\n", $content); // explode into array $content = array_reverse($content ); // reverse the array foreach($content as $temp=>$value) { $findme = 'Googlebot'; $googlebot = strpos($value, $findme); $findme1 = 'picsearch.com/bot'; $picsearch = strpos($value, $findme1); $findme2 = '/sys-includes'; $sysincludes = strpos($value, $findme2); $findme3 = '/offers/Scat'; $offersscat = strpos($value, $findme3); $findme4 = '/revelex'; $revelex = strpos($value, $findme4); $findme5 = '.gif'; $gif = strpos($value, $findme5); $findme6 = '.jpg'; $jpg = strpos($value, $findme6); $findme7 = '.txt'; $txt = strpos($value, $findme7); $findme8 = '.swf'; $swf = strpos($value, $findme8); $findme9 = '.dll'; $dll = strpos($value, $findme9); $findme10 = '.asp'; $asp = strpos($value, $findme10); $findme11 = '.png'; $png = strpos($value, $findme11); $findme12 = '.css'; $css = strpos($value, $findme12); $findme13 = '.ico'; $ico = strpos($value, $findme13); $findme14 = '.js'; $js = strpos($value, $findme14); $findme15 = '- 200'; $error = strpos($value, $findme15); $findme16 = 'msnbot'; $msnbot = strpos($value, $findme16); $findme17 = 'gigablast.com/spider.html'; $gigaspider = strpos($value, $findme17); $findme18 = 'InternetSeer.com'; $internetSeer = strpos($value, $findme18); $findme19 = '+Slurp'; $yahoo = strpos($value, $findme19); $findme20 = '....../1.0+'; $msnsneakbot = strpos($value, $findme20); $firstcharacter = substr($value, 0, 1); //strip lines beginning with #/null $weberror = substr($value, -18, 3); //strip lines beginning with 400/401/403/404/503 if($googlebot === false && $picsearch === false && $sysincludes === false && $offersscat === false && $revelex === false && $firstcharacter != "#" && $firstcharacter != "" && $weberror != "400" && $weberror != "401" && $weberror != "403" && $weberror != "404" && $weberror != "503" && $gif === false && $jpg === false && $txt === false && $swf === false && $dll === false && $asp === false && $png === false && $css === false && $ico === false && $js === false && $error == true && $msnbot === false && $gigaspider === false && $internetSeer === false && $yahoo === false && $msnsneakbot === false) { //echo "$value<br/><br/>"; $bits = explode(" ", $value); // CLIENT IP // $bits[9] $log_clientIP[] = $bits[9]; // CLIENT REQUEST // $bits[5] $log_request[] = $bits[5]; // Now sort out time range return $hour = substr($value, 11, 2); $hour = $hour *60; $minute = substr($value, 14, 2); $minutes = $minute + $hour; // These are the minutes for this line echo "$bits[9] My IP <br/>"; echo "$minutes - The time (as minutes)<br/>"; //echo the minutes for this day. echo "$bits[1] The time<br/><br/>"; //$find = $log_clientIP; if (in_array("$log_clientIP", $bits[-1])){ echo "$key"; // Therefore check if the compared line has the time diff of <7 mins ($minutes) if so then echo ip and visited page else ignore } /*if (strpos($log_clientIP[], $find) == false){ echo 'This ip does not currently exist'; // Therefore echo the ip and the visited page }*/ } // end of if stripper statement else{} } //end of foreach $value command fclose($fp); } //end of original if file exists statement else { echo 'This file does not exist!'; } ?> And here is an example of the log file its reading in 2007-11-11 00:05:06 W3SVC10019 xx.xxx.xxx.xxx GET /destinations/mexico/index.php - 80 - 66.249.65.193 HTTP/1.1 Mozilla/5.0+(compatible;+Googlebot/2.1;++http://www.google.com/bot.html) - - 200 0 22578 257 390 Thanks in advance! (edited by kenrbnsn to use tags) Quote Link to comment Share on other sites More sharing options...
eon201 Posted November 14, 2007 Author Share Posted November 14, 2007 Yes! Ive solved it! It was actually much easier than I thought. There is a lot to be said for just coming away from your computer sitting down and working around the problem on paper. If anyone comes across this post searching for answers here is my code! $filename = 'logs/'. 'ex'. date('ymd'). '.log'; // Change the path to your own log file! if (file_exists($filename)) // Does the corresponding filename exist - if yes then { $fp = fopen($filename, "r"); //Open the server log $content = fread($fp, filesize($filename)); // Read the server log $content = explode("\n", $content); // explode into array $content = array_reverse($content ); // reverse the array $ip = ""; $minutes =0; $prevurl = ""; foreach($content as $key=>$value) { $findme = 'Googlebot'; $googlebot = strpos($value, $findme); $findme1 = 'picsearch.com/bot'; $picsearch = strpos($value, $findme1); $findme2 = '/sys-includes'; $sysincludes = strpos($value, $findme2); $findme3 = '/offers/Scat'; $offersscat = strpos($value, $findme3); $findme4 = '/revelex'; $revelex = strpos($value, $findme4); $findme5 = '.gif'; $gif = strpos($value, $findme5); $findme6 = '.jpg'; $jpg = strpos($value, $findme6); $findme7 = '.txt'; $txt = strpos($value, $findme7); $findme8 = '.swf'; $swf = strpos($value, $findme8); $findme9 = '.dll'; $dll = strpos($value, $findme9); $findme10 = '.asp'; $asp = strpos($value, $findme10); $findme11 = '.png'; $png = strpos($value, $findme11); $findme12 = '.css'; $css = strpos($value, $findme12); $findme13 = '.ico'; $ico = strpos($value, $findme13); $findme14 = '.js'; $js = strpos($value, $findme14); $findme15 = '- 200'; $error = strpos($value, $findme15); $findme16 = 'msnbot'; $msnbot = strpos($value, $findme16); $findme17 = 'gigablast.com/spider.html'; $gigaspider = strpos($value, $findme17); $findme18 = 'InternetSeer.com'; $internetSeer = strpos($value, $findme18); $findme19 = '+Slurp'; $yahoo = strpos($value, $findme19); $findme20 = '....../1.0+'; $msnsneakbot = strpos($value, $findme20); $firstcharacter = substr($value, 0, 1); //strip lines beginning with #/null $weberror = substr($value, -18, 3); //strip lines beginning with 400/401/403/404/503 if($googlebot === false && $picsearch === false && $sysincludes === false && $offersscat === false && $revelex === false && $firstcharacter != "#" && $firstcharacter != "" && $weberror != "400" && $weberror != "401" && $weberror != "403" && $weberror != "404" && $weberror != "503" && $gif === false && $jpg === false && $txt === false && $swf === false && $dll === false && $asp === false && $png === false && $css === false && $ico === false && $js === false && $error == true && $msnbot === false && $gigaspider === false && $internetSeer === false && $yahoo === false && $msnsneakbot === false) { $bits = explode(" ", $value); $overall_array = array("ip" => "$bits[9]", "time" => "$bits[1]", "url" => "$bits[5]"); $hour = substr($bits[1], 0, 2); $hour = $hour *60; $minute = substr($bits[1], 3, 2); $minute = $minute + $hour; // These are the minutes for this line $theminutes = $minutes - $minute; if ($ip != $bits[9]){//If ip is not duplicate $ip = $overall_array['ip'];// Pass this to sql with time and url $time = $overall_array['time']; $url = $overall_array['url']; echo "$theminutes - $ip - $time - $url<br/><br/>"; } elseif ($ip = $bits[9] && $theminutes <=7 && $prevurl = $url){// the ip is a duplicate && the time difference is under than 7 mins && the url is the same echo "This ip is a duplicate && the time difference from the last iteration is under 7 minutes && the url is the same as the last iteration. DONT PASS TO MYSQL<br/><br/>"; $url = $overall_array['url']; $ip = $overall_array['ip'];// Pass this to sql with time and url } elseif ($ip = $bits[9] && $theminutes >=7){// the ip is a duplicate and the time difference is over than 7 mins $ip = "This is a duplicate ip but the time difference is over 7 mins"; $time = $overall_array['time']; $url = $overall_array['url']; echo "$theminutes - $ip - $time - $url<br/><br/>"; } $minutes = $minute; $prevurl = $url; }//end of if strip statement }//end of first foreach loop }//end of if file exists ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.