magento 记录蜘蛛爬行日志

<?php  
 function isSpider(){
    $bots = array(
                    'google'    => 'googlebot',
                    'yahoo'        => 'yahoo slurp',
                    'soso'        => 'sosospider',
                    'msn'        => 'msnbot',
                    'altavista'    => 'scooter',
                    'sogou'        => 'sogou spider',
                    'bing'        => 'bing',
                    'yodao'        => 'yodaobot',
                    
                    
            );
    $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
    foreach ($bots as $k => $v){
    
        if (strstr($userAgent,$k)){
            return $v;
            break;
        }
    }
    return false;
}
$spi= isSpider();
if($spi){
    $tlc_thispage    = addslashes($_SERVER['HTTP_USER_AGENT']);
    //$file            = 'r.txt';
    $time            = date('Y-m-d H:i:s',mktime());
    //$handle            = fopen($file,'a+');
    $PR                = $_SERVER['REQUEST_URI'];
mysql_connect("localhost","root","");//连接MySQL
mysql_select_db("db");//选择数据库
$sql = "insert into robots(website,robot,url,agent,time) values ('website','$spi','$PR','$tlc_thispage','$time')";
mysql_query($sql);//借SQL语句插入数据
mysql_close();//关闭MySQL连接
 //echo  "Time:{$time} ROBOT:{$spi} AGENT:{$tlc_thispage} URL:{$PR} \n\r";
   // fwrite($handle, "Time:{$time} ROBOT:{$spi} AGENT:{$tlc_thispage} URL:{$PR} \n\r");
    //fclose($handle);
}
?>

 

CREATE TABLE `robots` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `website` varchar(20) NOT NULL,
  `url` varchar(300) NOT NULL,
  `robot` varchar(20) NOT NULL,
  `time` datetime DEFAULT NULL,
  `agent` varchar(50) DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
 

你可能感兴趣的:(agent)