复制代码 代码如下:
<?php
define('IE', 1);
define('FIREFOX', 2);
define('CHROME', 3);
define('OPERA', 4);
class UserAgentDetect
{
    static $version = array();

    static $userAgent = '';

    static function getUserAgent()
    {
        $header = getallheaders();
        self :: $userAgent = $header['User-Agent'];
    }
  function findUserAgentByKey($ua, $key)
    {
        $len = strlen($key);
        $start = strpos($ua, $key);

        $pos = strpos($ua, ' ', $start + $len + 1);

        return substr($ua, $start + $len, $pos - $len - $start);
    }
    static function detectUserAgent()
    {
         self :: getUserAgent();

        if (strpos(self :: $userAgent, 'MSIE') != null)
        {
            self :: $version[] = IE;
            self :: $version[] =self:: findUserAgentByKey(self :: $userAgent, 'MSIE');
        }
        else if (strpos(self :: $userAgent, 'Gecko/') != null)
        {

            self :: $version[] = FIREFOX;
            self :: $version[] =self::  findUserAgentByKey(self :: $userAgent, 'Firefox/');
        }
        else if (strpos(self :: $userAgent, 'AppleWebKit/') != null)
        {
            self :: $version[] = CHROME;
            self :: $version[] = self:: findUserAgentByKey(self :: $userAgent, 'Chrome/');
        }
        else if (strpos(self :: $userAgent, 'Presto/') != null)
        {
            self :: $version[] = OPERA;
            self :: $version[] = self:: findUserAgentByKey(self :: $userAgent, 'Opera/');
        }
        else
        {
        }

        return self :: $version;
    }


    function matchUserAgent($ua, $key)
    {
        preg_match("/$key.([^\s]*?)\s/", $ua, $out);

        return $out[1];
    }
}
$ua=UserAgentDetect::detectUserAgent();

print_r($ua);

标签:
检测useragent

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

评论“php检测useragent版本示例”

暂无“php检测useragent版本示例”评论...