程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> php檢測useragent版本示例

php檢測useragent版本示例

編輯:PHP綜合

復制代碼 代碼如下:
<?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);

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved