一般来说,站长都希望掌握蜘蛛在网站上的运动痕迹,如果你会一点PHP知识,那么这个功能就会很容易实现了。我们可以通过User-Agent数据进行识别,虽然User-Agent可以伪造,但是我们可以通过User-Agent和IP地址对真假蜘蛛的行为进行判断。


我们想要掌握蜘蛛爬取的页面,并把相关信息保存下来,我们先来获取一些关键信息。

1、获取当前页面URL


function
getCurrentURL()
{
   $protocol = $_SERVER["SERVER_PORT"] == '443' ? 'https://' : 'http://';
   return $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}


2、获取User-Agent


//
获取User-Agent字符串
$userAgent = $_SERVER['HTTP_USER_AGENT'];


3、获取IP地址


//
获取访问者的IP地址
$ipAddress = $_SERVER['REMOTE_ADDR'];


4、蜘蛛爬取时间

$time = time();


4、识别蜘蛛名称


function
isbot($tmp){
  //谷歌蜘蛛
 if(strpos($tmp, 'compatible; Googlebot/2.1') !== false){$flag = '谷歌蜘蛛';}
   else if(strpos($tmp, 'Googlebot-Mobile') >0){$flag = '谷歌蜘蛛';}
   else if(strpos($tmp, 'Googlebot-Image') >0){$flag = '谷歌图片蜘蛛';}
   else if(strpos($tmp, 'Mediapartners-Google') >0){$flag = '谷歌广告蜘蛛';}
   else if(strpos($tmp, 'Adsbot-Google') >0){$flag = '谷歌质量蜘蛛';}
   else if(strpos($tmp, 'Googlebot') >0){$flag = '谷歌蜘蛛';}
   //百度蜘蛛
 else if(strpos($tmp, 'Baiduspider-mobile') >0){$flag = '百度蜘蛛';}
   else if(strpos($tmp, 'Baidu-Thumbnail') >0){$flag = '百度图片蜘蛛';}
   else if(strpos($tmp, 'Baiduspider-image') >0){$flag = '百度图片蜘蛛';}
   else if(strpos($tmp, 'Baiduspider-news') >0){$flag = '百度新闻蜘蛛';}
   else if(strpos($tmp, 'Baiduspider-video') >0){$flag = '百度视频蜘蛛';}
   else if(strpos($tmp, 'Baidu-Transcoder') >0){$flag = '百度音乐蜘蛛';}
   else if(strpos($tmp, 'baiduspider-mobile-gate') >0){$flag = '百度移动蜘蛛';}
   else if(strpos($tmp, 'Baiduspider') >0){$flag = '百度蜘蛛';}
   //搜搜蜘蛛
  else if(strpos($tmp, 'Sosospider') >0){$flag = '搜搜蜘蛛';}
   else if(strpos($tmp, 'Sosoimagespider') >0){$flag = '搜搜图片蜘蛛';}
   //雅虎蜘蛛
 else if(strpos($tmp, 'Yahoo! Slurp China') !== false){$flag = '雅虎中文蜘蛛';}
   else if(strpos($tmp, 'Yahoo ContentMatch Crawler') !== false){$flag = '雅虎竞价蜘蛛';}
   else if(strpos($tmp, 'Yahoo-MMCrawler') !== false){$flag = '雅虎图片蜘蛛';}
   else if(strpos($tmp, 'Yahoo! Slurp') !== false){$flag = '雅虎英文蜘蛛';}
   //微软蜘蛛
 else if(strpos($tmp, 'msnbot') !== false){$flag = '微软蜘蛛';}
   else if(strpos($tmp, 'msnbot-media') !== false){$flag = '微软媒体蜘蛛';}
   else if(strpos($tmp, 'MSNBot-Media') !== false){$flag = '微软多媒体蜘蛛';}
   else if(strpos($tmp, 'MSNBot-NewsBlogs') !== false){$flag = '微软新闻及blog蜘蛛';}
   else if(strpos($tmp, 'MSNBot-Academic') !== false){$flag = '微软学术蜘蛛';}
   else if(strpos($tmp, 'MSNBot') !== false){$flag = '微软网页蜘蛛';}
   //360蜘蛛
 else if(strpos($tmp, 'Sosospider') !== false){$flag = '360蜘蛛';}
    //有道蜘蛛
 else if(strpos($tmp, 'YodaoBot') !== false || strpos($tmp, 'OutfoxBot') !== false){$flag = '有道蜘蛛';}
    //搜狗蜘蛛
 else if(strpos($tmp, 'Sogou web spider') !== false || strpos($tmp, 'Sogou Orion spider') !== false){$flag = '搜狗蜘蛛';}
   else if(strpos($tmp, 'Sogou inst spider') !== false){$flag = '搜狗蜘蛛';}
   else if(strpos($tmp, 'Sogou News Spider') !== false){$flag = '搜狗新闻蜘蛛';}
   else if(strpos($tmp, 'Sogou spider2') !== false){$flag = '搜狗蜘蛛';}
   else if(strpos($tmp, 'Sogou blog') !== false){$flag = '搜狗blog蜘蛛';}
   else if(strpos($tmp, 'sogou spider') !== false){$flag = '搜狗蜘蛛';}
   //其他蜘蛛
 else if(strpos($tmp, 'bingbot') !== false){$flag = '必应蜘蛛';}
   else if(strpos($tmp, 'EtaoSpider') !== false){$flag = '一淘网蜘蛛';}
   else if(strpos($tmp, 'Scooter') !== false){$flag = 'Altavista蜘蛛';}
   else if(strpos($tmp, 'Lycos_Spider') !== false){$flag = 'Lycos蜘蛛';}
   else if(strpos($tmp, 'FAST-WebCrawler') !== false){$flag = 'Alltheweb蜘蛛';}
   else if(strpos($tmp, 'Slurp ASPSeek ASPSeek') !== false){$flag = 'INKTOMI蜘蛛';}
   else if(strpos($tmp, 'lanshanbot') !== false){$flag = '东方网景爬虫';}
   else if(strpos($tmp, 'BSpider') !== false){$flag = '日本爬虫';}
   else if(strpos($tmp, 'fast-webcrawler') !== false){$flag = 'fast-webcrawler';}
   else if(strpos($tmp, 'Gaisbot') !== false){$flag = 'Gaisbot';}
   else if(strpos($tmp, 'ia_archiver') !== false){$flag = 'Alexa蜘蛛';}
   else if(strpos($tmp, 'altavista') !== false){$flag = 'altavista爬虫';}
   else if(strpos($tmp, 'lycos_spider') !== false){$flag = 'Lycos蜘蛛';}
   else if(strpos($tmp, 'Inktomi slurp') !== false){$flag = 'Inktomi slurp';}
   else if(strpos($tmp, 'YandexBot') !== false){$flag = 'YandexBot';}
   else if(strpos($tmp, 'AhrefsBot') !== false){$flag = 'AhrefsBot';}
   else if(strpos($tmp, 'ezooms.bot') !== false){$flag = 'ezooms.bot';}
   else{$flag = NULL;}
   return $flag;
}


6、完整案例

我们首先把获取URL和识别蜘蛛的类放在bot.class.php文件里,方便后续实例化这些类!


require
'bot.class.php';
$userAgent = $_SERVER['HTTP_USER_AGENT'];//获取访问者头部信息,该信息由浏览器发送到服务器,有可能被人为修改。
$botname = isbot($userAgent);
//判断访问者是不是蜘蛛
if ($botname) !== NULL){
   $time = time();
   $ip = $_SERVER['REMOTE_ADDR'];
   $url = getCurrentURL();
//蜘蛛名称$botname
//===========
   这里你就可以把以上变量存入数据库或者文件,方便后续读取!
如果你需要掌握更多信息,可以自己进行完善
//===========
}

注意:以上isbot()类包括了全网最新最全蜘蛛的特征,如果你发现还有别的新的蜘蛛,欢迎留言补充!