eon201 Posted November 13, 2007 Share Posted November 13, 2007 Hey, First off. My name is eon201 and I am a new/noob member! So my problem is this. Im creating a small stats counter (god knows why after all the trouble it ahs caused me!) for my site, and getting all of the relevant data from the server log files. Getting the data isnt a problem, and slicing it up is fine but.. Ive created a rule that is trying to emulate a google analytics feature (the feature will count a visitor twice if they return after a certain amount of minutes even if its the same ip), but the code doesnt work. There are loads of different ip's in the log so I just cant understand why the code is interpreting just one ip. Here's the code. $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 "$minutes - The time (as minutes)<br/>"; //echo the minutes for this day. echo "$bits[1] The time<br/>"; $uniqueip[] = array_unique($log_clientIP); if ($log_clientIP = $uniqueip){ echo "This is a unique ip<br/><br/>"; // Then count this as a new visitor and an addittion to log_request } elseif ($log_clientIP != $uniqueip){ $mylogtime = $minutes[$temp -1] - $minutes; echo "$mylogtime = The sum of this time loop - the last time loop<br/><br/><br/>"; if ($mylogtime >=7){ // Then count this as a (returning but new) visitor and an addittion to log_request } else{} Heres (a slice of) the output. 1410 - The time (as minutes) 23:30:27 The time This is a unique ip 1410 - The time (as minutes) 23:30:25 The time This is a unique ip 1383 - The time (as minutes) 23:03:39 The time This is a unique ip 1368 - The time (as minutes) 22:48:45 The time This is a unique ip 1367 - The time (as minutes) 22:47:05 The time This is a unique ip 1329 - The time (as minutes) 22:09:07 The time This is a unique ip 1328 - The time (as minutes) 22:08:43 The time This is a unique ip 1322 - The time (as minutes) 22:02:35 The time This is a unique ip 1315 - The time (as minutes) 21:55:58 The time This is a unique ip 1311 - The time (as minutes) 21:51:24 The time This is a unique ip 1308 - The time (as minutes) 21:48:50 The time This is a unique ip 1277 - The time (as minutes) 21:17:16 The time This is a unique ip 1265 - The time (as minutes) 21:05:07 The time This is a unique ip 1251 - The time (as minutes) 20:51:34 The time This is a unique ip 1250 - The time (as minutes) 20:50:20 The time This is a unique ip As you can see it doesnt seem to get to my elseif statement. Why?? Can someone please help. Ive been sat for ten hours today trying to solve this mother! ps. the code is in a foreach loop statement. But I thought it was unessecary to include it. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 13, 2007 Share Posted November 13, 2007 Use a "==" for comparison, "=" for assignment: <?php if ($log_clientIP = $uniqueip) ?> Ken Quote Link to comment Share on other sites More sharing options...
eon201 Posted November 13, 2007 Author Share Posted November 13, 2007 sorry kenrbnsn. I am a proper noob... Can you explain that in more (if even possible) plain detail?? Thanks for the quick response! Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 13, 2007 Share Posted November 13, 2007 Replace <?php if ($log_clientIP = $uniqueip) ?> with <?php if ($log_clientIP == $uniqueip) ?> Ken Quote Link to comment Share on other sites More sharing options...
eon201 Posted November 13, 2007 Author Share Posted November 13, 2007 Ok ive done that. It now outputs.. 1410 - The time (as minutes) 23:30:27 The time -1410 = The sum of this time loop - the last time loop 1410 - The time (as minutes) 23:30:25 The time -1410 = The sum of this time loop - the last time loop 1383 - The time (as minutes) 23:03:39 The time -1383 = The sum of this time loop - the last time loop 1368 - The time (as minutes) 22:48:45 The time -1368 = The sum of this time loop - the last time loop 1367 - The time (as minutes) 22:47:05 The time -1367 = The sum of this time loop - the last time loop 1329 - The time (as minutes) 22:09:07 The time -1329 = The sum of this time loop - the last time loop 1328 - The time (as minutes) 22:08:43 The time -1328 = The sum of this time loop - the last time loop Which means its not doing this sum now $minutes[$temp -1] - $minutes but this instead?? $minutes -$minutes - $minutes Very strange. ??? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 13, 2007 Share Posted November 13, 2007 You're trying to use $minutes as both a scalar and an array at the same time here: <?php $mylogtime = $minutes[$temp -1] - $minutes; ?> That is not a good practice. Where are you storing the previous contents of "$minutes"? Also, the code <?php elseif ($log_clientIP != $uniqueip){ ?> can just be written as <?php else { ?> Ken Quote Link to comment Share on other sites More sharing options...
eon201 Posted November 13, 2007 Author Share Posted November 13, 2007 Ok. Thanks kenrbnsn. Yer I thought I could use the else statement, just been trying soooo many different ways to get this to work! The $minutes comes straight from the log file which looks like this for the first bunch of characters... 2007-11-11 00:00:38 W3SVC10019 I used substr function to grab the hours and minutes. I then multiplied the hours by 60 to turn them into minutes then added both the $hours and $minutes variables to get the overall amount of minutes (I hope that makes sense!). The previous contents of $minutes ($minutes[$temp -1]) I thought was created by the foreach statement that this is all nested in as it looped through?? Am I massivly wrong here? ??? ps. Just to stop confusion here's the whole script. <?php set_time_limit(30); // Run this script every 15 minutes! // Do workings for time to be parsed // What time was the script last run? $timefile = 'time.txt'; $timeopen = fopen($timefile, "r"); $thentiming = fread($timeopen, filesize($timefile)); fclose($timeopen); // whats the time Mr Wolf? $nowtiming = date('Gis'); //whats the time difference $timedifference = $nowtiming - $thentiming; // Now update last run time in file $timefile2 = 'time.txt'; $timeopen2 = fopen($timefile, "w"); fwrite($timeopen2, $nowtiming); fclose($timeopen2); $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 "$minutes - The time (as minutes)<br/>"; //echo the minutes for this day. echo "$bits[1] The time<br/>"; $uniqueip[] = array_unique($log_clientIP); if ($log_clientIP == $uniqueip){ echo "This is a unique ip<br/><br/>"; // Then count this as a new visitor and an addittion to log_request } elseif ($log_clientIP != $uniqueip){ $mylogtime = $minutes[$temp -1] - $minutes; echo "$mylogtime = The sum of this time loop - the last time loop<br/><br/><br/>"; if ($mylogtime >=7){ // Then count this as a (returning but new) visitor and an addittion to log_request } else{} }// end of elseif statement } // 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!'; } ?> Quote Link to comment Share on other sites More sharing options...
eon201 Posted November 13, 2007 Author Share Posted November 13, 2007 oh by the way please ignore the time stuff that writes to a file at the top of the code. This is now redundant. And I just havnt taken it out yet! And I know that the massive if rule is badly done. But im a messy noob coder! Quote Link to comment Share on other sites More sharing options...
jscix Posted November 13, 2007 Share Posted November 13, 2007 I didn't fix anything FYI. I don't totally understand what you're trying to do, but I figured this might help anyone who does - Much prettier code .. <?php set_time_limit (30); // Run this script every 15 minutes! // Do workings for time to be parsed // What time was the script last run? $timefile = 'time.txt'; $timeopen = fopen($timefile, "r"); $thentiming = fread($timeopen, filesize($timefile)); fclose ($timeopen); // whats the time Mr Wolf? $nowtiming = date('Gis'); //whats the time difference $timedifference = $nowtiming - $thentiming; // Now update last run time in file $timefile2 = 'time.txt'; $timeopen2 = fopen($timefile, "w"); fwrite($timeopen2, $nowtiming); fclose ($timeopen2); $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 "$minutes - The time (as minutes)<br/>"; //echo the minutes for this day. echo "$bits[1] The time<br/>"; $uniqueip[] = array_unique($log_clientIP); if ($log_clientIP == $uniqueip) { echo "This is a unique ip<br/><br/>"; // Then count this as a new visitor and an addittion to log_request } elseif ($log_clientIP != $uniqueip) { $mylogtime = $minutes[$temp - 1] - $minutes; echo "$mylogtime = The sum of this time loop - the last time loop<br/><br/><br/>"; if ($mylogtime >= 7) { // Then count this as a (returning but new) visitor and an addittion to log_request } else { } } // end of elseif statement } // 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!'; } ?> Quote Link to comment Share on other sites More sharing options...
eon201 Posted November 13, 2007 Author Share Posted November 13, 2007 I havnt really explained what I am trying to do well enough here. Let me explain step by step what im trying to do! Search for a specific file name. (We are working with a server log file) Does it exist? Yes.Continue. No.Error Open the file Read the file Explode it into seperate lines Reverse it (therefore getting the most recent data at the top) Create a loop state all the bad stuff that i want the code to find and if it does ignore the line of data Get variables for the client ip's and client url requested Get the time for hours and minutes and convert to just minutes from log if the ip is unique; then output the url and ip (which i will later send to mysql) if the ip is not unique but the time difference between the last visit to this page from the same ip was moe than 7 minutes ago; then output the url and ip (which i will later send to mysql). And thats about it! I know its weird. And it fries my brain. Especially when its my first project. I hope this makes things more clear! Thankyou very very much for the help so far! Quote Link to comment Share on other sites More sharing options...
eon201 Posted November 14, 2007 Author Share Posted November 14, 2007 Can anybody help me with this?? Im still absolutely stuck, and ive been trying ot get it too work for days now. Any help is always greatly appreciatted! Thanks eon201. Quote Link to comment Share on other sites More sharing options...
eon201 Posted November 14, 2007 Author Share Posted November 14, 2007 Ok. so I have managed to get this far now... My script is now in this state.. <?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 "$minutes - The time (as minutes)<br/>"; //echo the minutes for this day. echo "$bits[1] The time<br/>"; $uniqueip[] = array_unique($log_clientIP); if ($log_clientIP == $uniqueip){ echo "This is a unique ip<br/><br/>"; // Then count this as a new visitor and an addittion to log_request } elseif ($log_clientIP != $uniqueip){ $previouskey[] = $temp[-1]; $lastminutes = $previouskey[1]; $mylogtime = $lastminutes - $minutes; echo "$mylogtime = The sum of this time loop - the last time loop<br/><br/><br/>"; if ($mylogtime >=7){ // Then count this as a (returning but new) visitor and an addittion to log_request } else{} }// end of elseif statement } // 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!'; } ?> but the output is this... 1410 - The time (as minutes) 23:30:27 The time Notice: Undefined offset: 1 in D:\home\Default\planetcruise-svr.co.uk\htdocs\pcm\logreader3.php on line 145 -1410 = The sum of this time loop - the last time loop 1410 - The time (as minutes) 23:30:25 The time -1410 = The sum of this time loop - the last time loop 1383 - The time (as minutes) 23:03:39 The time -1383 = The sum of this time loop - the last time loop 1368 - The time (as minutes) 22:48:45 The time -1368 = The sum of this time loop - the last time loop 1367 - The time (as minutes) 22:47:05 The time -1367 = The sum of this time loop - the last time loop 1329 - The time (as minutes) 22:09:07 The time -1329 = The sum of this time loop - the last time loop 1328 - The time (as minutes) 22:08:43 The time -1328 = The sum of this time loop - the last time loop 1322 - The time (as minutes) 22:02:35 The time -1322 = The sum of this time loop - the last time loop 1315 - The time (as minutes) 21:55:58 The time -1315 = The sum of this time loop - the last time loop 1311 - The time (as minutes) 21:51:24 The time -1311 = The sum of this time loop - the last time loop 1308 - The time (as minutes) 21:48:50 The time -1308 = The sum of this time loop - the last time loop 1277 - The time (as minutes) 21:17:16 The time -1277 = The sum of this time loop - the last time loop 1265 - The time (as minutes) 21:05:07 The time -1265 = The sum of this time loop - the last time loop 1251 - The time (as minutes) 20:51:34 The time -1251 = The sum of this time loop - the last time loop 1250 - The time (as minutes) 20:50:20 The time -1250 = The sum of this time loop - the last time loop 1234 - The time (as minutes) 20:34:38 The time -1234 = The sum of this time loop - the last time loop 1188 - The time (as minutes) 19:48:59 The time -1188 = The sum of this time loop - the last time loop 1187 - The time (as minutes) 19:47:52 The time -1187 = The sum of this time loop - the last time loop 1183 - The time (as minutes) 19:43:19 The time -1183 = The sum of this time loop - the last time loop 1182 - The time (as minutes) 19:42:51 The time -1182 = The sum of this time loop - the last time loop I belive the problem with the time is within this part of the code.. $uniqueip[] = array_unique($log_clientIP); if ($log_clientIP == $uniqueip){ echo "This is a unique ip<br/><br/>"; // Then count this as a new visitor and an addittion to log_request } elseif ($log_clientIP != $uniqueip){ $previouskey[] = $temp[-1]; $lastminutes = $previouskey[1]; $mylogtime = $lastminutes - $minutes; echo "$mylogtime = The sum of this time loop - the last time loop<br/><br/><br/>"; if ($mylogtime >=7){ // Then count this as a (returning but new) visitor and an addittion to log_request } else{} }// end of elseif statement Can anybody help me please?? Im getting very desperate! ??? 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.