PHP判断是不是搜索引擎蜘蛛

远昔 代码记录 2021-08-15 14193 0

PHP判断来访者是不是搜索引擎蜘蛛

<?
function is_spider($ip=''){ 
    $userAgent = strtolower($_SERVER['HTTP_USER_AGENT'] ?? ''); //检查User-Agent
    $spiders = array( 
        'spider',
        'Googlebot', 
        'Baiduspider', 
        'sogou spider', 
        'sogou web',
        '360spider',
        'YisouSpider', //神马
        'YodaoBot',   //有道
        'Bytespider', //头条
        'bing',//必应
        'bot'
    ); 
foreach ($spiders as $spider){ $spider=strtolower($spider);//小写
    if(strpos($userAgent, $spider) !== false) { return true;  } }
 
if($ip==null){ $ip=$_SERVER['REMOTE_ADDR']; }
//2025年最新百度蜘蛛IP段正则表达式
$baidu_ip_regex = [
        // 山西阳泉节点
        '/^116\.179\.[0-9]{1,3}\.[0-9]{1,3}$/',
        '/^124\.164\.[0-9]{1,3}\.[0-9]{1,3}$/',
        '/^124\.167\.[0-9]{1,3}\.[0-9]{1,3}$/',
        
        // 北京节点
        '/^123\.125\.[0-9]{1,3}\.[0-9]{1,3}$/',
        '/^220\.181\.[0-9]{1,3}\.[0-9]{1,3}$/',
        '/^111\.206\.[0-9]{1,3}\.[0-9]{1,3}$/',
        '/^180\.76\.[0-9]{1,3}\.[0-9]{1,3}$/',
        
        // 其他节点
        '/^36\.110\.[0-9]{1,3}\.[0-9]{1,3}$/',
        '/^112\.65\.[0-9]{1,3}\.[0-9]{1,3}$/',
        '/^115\.239\.[0-9]{1,3}\.[0-9]{1,3}$/',
        '/^119\.63\.[0-9]{1,3}\.[0-9]{1,3}$/',
        
        // 2025新增段
        '/^106\.12\.[0-9]{1,3}\.[0-9]{1,3}$/',
        '/^139\.159\.[0-9]{1,3}\.[0-9]{1,3}$/',
        '/^47\.119\.(12[8-9]|1[3-5][0-9]|16[0-3])\.[0-9]{1,3}$/'
    ];
    // 检查IP是否匹配任一正则表达式
    foreach ($baidu_ip_regex as $regex) {
if(preg_match($regex, $ip)) { return true; } }
    
     return false;}
if  (is_spider()){ echo '是蜘蛛';}?>


评论

发表评论:

挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
提交评论

清空信息
关闭评论