欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php检测useragent版本示例

程序员文章站 2022-06-20 23:46:39
复制代码 代码如下:

复制代码 代码如下:

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