php检测useragent版本的代码

发布时间:2020-06-01编辑:脚本学堂
本文介绍了php检测useragent版本的实现代码,用php检测浏览器中的useragent信息,写成了一个类,有需要的朋友参考下。

例子,php检测useragent版本信息。
 
代码:
 

复制代码 代码示例:

<?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);
    }

    // useragent版本信息检测
    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/');
        } // www.jb200.com
        else if (strpos(self :: $userAgent, 'Presto/') != null)
        {
            self :: $version[] = OPERA;
            self :: $version[] = self:: findUserAgentByKey(self :: $userAgent, 'Opera/');
        }
        else
        {
        }
        return self :: $version;
    }
   
    //useragent版本信息匹配
    function matchUserAgent($ua, $key)
    {
        preg_match("/$key.([^s]*?)s/", $ua, $out);
        return $out[1];
    }
}
$ua=UserAgentDetect::detectUserAgent();
print_r($ua);

您可能感兴趣的文章: