php 判斷訪客是否為搜尋引擎蜘蛛的函數程式碼

高洛峰
發布: 2023-03-05 08:12:01
原創
2236 人瀏覽過

/** 
* 判断是否为搜索引擎蜘蛛 
* 
* @author Eddy 
* @return bool 
*/ 
function isCrawler() { 
$agent= strtolower($_SERVER['HTTP_USER_AGENT']); 
if (!empty($agent)) { 
$spiderSite= array( 
"TencentTraveler", 
"Baiduspider+", 
"BaiduGame", 
"Googlebot", 
"msnbot", 
"Sosospider+", 
"Sogou web spider", 
"ia_archiver", 
"Yahoo! Slurp", 
"YoudaoBot", 
"Yahoo Slurp", 
"MSNBot", 
"Java (Often spam bot)", 
"BaiDuSpider", 
"Voila", 
"Yandex bot", 
"BSpider", 
"twiceler", 
"Sogou Spider", 
"Speedy Spider", 
"Google AdSense", 
"Heritrix", 
"Python-urllib", 
"Alexa (IA Archiver)", 
"Ask", 
"Exabot", 
"Custo", 
"OutfoxBot/YodaoBot", 
"yacy", 
"SurveyBot", 
"legs", 
"lwp-trivial", 
"Nutch", 
"StackRambler", 
"The web archive (IA Archiver)", 
"Perl tool", 
"MJ12bot", 
"Netcraft", 
"MSIECrawler", 
"WGet tools", 
"larbin", 
"Fish search", 
); 
foreach($spiderSite as $val) { 
$str = strtolower($val); 
if (strpos($agent, $str) !== false) { 
return true; 
} 
} 
} else { 
return false; 
} 
}
登入後複製

網上倒是能搜到一大把,不過都是千篇一律的複製來複製去的,也不夠全面,我這裡整理了一份比較全面的代碼: 

function is_spider(){ 
$robot = 0; 
$USER_AGENT = strtolower($_SERVER['HTTP_USER_AGENT']); 
if(strpos($USER_AGENT,"bot")) $robot = 1; 
if(strpos($USER_AGENT,"spider")) $robot = 1; 
if(strpos($USER_AGENT,"slurp")) $robot = 1; 
if(strpos($USER_AGENT,"mediapartners-google")) $robot = 1; 
if(strpos($USER_AGENT,"fast-webcrawler")) $robot = 1; 
if(strpos($USER_AGENT,"altavista")) $robot = 1; 
if(strpos($USER_AGENT,"ia_archiver")) $robot = 1; 
if($robot == 1){ 
//do something 
} 
return ''; 
}
登入後複製

更多php 判斷訪客是否為搜尋引擎蜘蛛的函數代碼相關文章請關注PHP中文網!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板