strago Posted September 13, 2010 Share Posted September 13, 2010 How do you have a log generate two different logs, and having where it posts the log at, depend on if it's a visitor, or a search engine spider HTTP_USER_AGENT (Like Googlebot, Msnbot, Yahoo! Slurp.) <?php define("DATE_FORMAT","m-d-Y - H:i:s"); define("LOG_FILE","/full_path/visitors.html"); define("LOG_FILE2","/full_path/search_engine_bots.html"); $logfileHeader='DATE - IP - HOSTNAME - BROWSER - URI - REFERRER'."\n"; $userAgent = (isset($_SERVER['HTTP_USER_AGENT']) && ($_SERVER['HTTP_USER_AGENT'] != "")) ? $_SERVER['HTTP_USER_AGENT'] : "Unknown"; $userIp = (isset($_SERVER['REMOTE_ADDR']) && ($_SERVER['REMOTE_ADDR'] != "")) ? $_SERVER['REMOTE_ADDR'] : "Unknown"; $refferer = (isset($_SERVER['HTTP_REFERER']) && ($_SERVER['HTTP_REFERER'] != "")) ? $_SERVER['HTTP_REFERER'] : "Unknown"; $uri = (isset($_SERVER['REQUEST_URI']) && ($_SERVER['REQUEST_URI'] != "")) ? $_SERVER['REQUEST_URI'] : "Unknown"; $hostName = gethostbyaddr($userIp); $actualTime = date(DATE_FORMAT); $logEntry = "$actualTime - $userIp - $hostName - $userAgent - $uri - $refferer<BR>\n"; if (!file_exists(LOG_FILE)) { $logFile = fopen(LOG_FILE,"w"); fwrite($logFile, $logfileHeader); } else { $logFile = fopen(LOG_FILE,"a"); } fwrite($logFile,$logEntry); fclose($logFile); ?> Quote Link to comment Share on other sites More sharing options...
schilly Posted September 13, 2010 Share Posted September 13, 2010 Ya just check the user agent for known crawlers. You should be able to find a list somewhere. http://www.user-agents.org/ Quote Link to comment Share on other sites More sharing options...
strago Posted September 13, 2010 Author Share Posted September 13, 2010 For a search engine only log, would I only need to change ($_SERVER['HTTP_USER_AGENT'] != "")) to ($_SERVER['HTTP_USER_AGENT'] != "Googlebot|MantraAgent|Slurp|WebCrawler|ia_archiver|FAST-WebCrawler")) and add an else for making a log that doesn't contain those? Quote Link to comment Share on other sites More sharing options...
schilly Posted September 13, 2010 Share Posted September 13, 2010 Ya that should work. Does using an OR in a string work? Never tried that before. 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.