php检测useragent版本示例

编程学习 2021-07-05 09:50www.dzhlxh.cn编程入门
这篇文章主要介绍了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);
    }
    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);

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板