天生我才必有用

搜索机器人的PHP代码

分类: PHP    作者:Ray    2008年10月21日
/**
* 判断是否为搜索引擎蜘蛛
*
* @access  public
* @return  string
*/
function is_spider($record = true)
{
    static $spider = NULL;
    if ($spider !== NULL)
    {
        return $spider;

    }
    if (empty($_SERVER['HTTP_USER_AGENT']))
    {
        $spider = '';
        return '';
    }

    $searchengine_bot = array(
        'googlebot',
        'mediapartners-google',
        'baiduspider+',
        'msnbot',
        'yodaobot',
        'yahoo! slurp;',
        'yahoo! slurp china;',
        'iaskspider',
        'sogou web spider',
        'sogou push spider',
        'feedfetcher-google',
        'yahoofeedseeker'
    );

    $searchengine_name = array(
        'GOOGLE',
        'GOOGLE ADSENSE',
        'BAIDU',
        'MSN',
        'YODAO',
        'YAHOO',
        'Yahoo China',
        'IASK',
        'SOGOU',
        'SOGOU',
        'Feedfetcher-Google',
        'YahooFeedSeeker'
    );

    $spider = strtolower($_SERVER['HTTP_USER_AGENT']);
    foreach ( $searchengine_bot AS $key => $value)
    {
        if (strpos($spider, $value) !== false)
        {
            $spider = $searchengine_name[$key];
            if ($record === true)
            {
            // do something
            }
            return $spider;
        }
    }
    $spider = '';
    return '';
}
标签: , ,

Leave a Reply