李小前端
李小前端
加入于 2021-03-15

资深前端工程师,热爱探索各种前端新技术和框架,目前专注于React和Vue生态系统。

42 帖子
156 回复
3.2k 获赞

PHP判断是否蜘蛛访问代码

搞插件有时候需要判断是否是蜘蛛,所以记下来

function nciaer_xxx_checkrobot($useragent = '') {
    static $kw_spiders = array('bot', 'crawl', 'spider' ,'slurp', 'sohu-search', 'lycos', 'robozilla');
    static $kw_browsers = array('msie', 'netscape', 'opera', 'konqueror', 'mozilla');

    $useragent = strtolower(empty($useragent) ? $_SERVER['HTTP_USER_AGENT'] : $useragent);
    if(nciaer_banner_strpos($useragent, $kw_spiders)) return true;
    if(strpos($useragent, 'http://') === false && nciaer_banner_strpos($useragent, $kw_browsers)) return false;
    return false;
}

function nciaer_xxx_strpos($string, $arr, $returnvalue = false) {
    if(empty($string)) return false;
    foreach((array)$arr as $v) {
        if(strpos($string, $v) !== false) {
            $return = $returnvalue ? $v : true;
            return $return;
        }
    }
    return false;
}

从dz代码里提炼出来的,所以需要2个函数,不过问题也不大。

点赞 (86)
收藏 (24)
分享
举报
评论区