Home Backend Development PHP Tutorial PHP获取客户端操作系统,游览器类型及版本号

PHP获取客户端操作系统,游览器类型及版本号

Jun 23, 2016 pm 01:18 PM

<?php/** * 客户端工具类 * * For example: * * clientUtil::getBrowser($_SERVER['HTTP_USER_AGENT'],'是否显示版本号')  //获取客户端游览器类型和版本号 * clientUtil::getPlatForm($_SERVER['HTTP_USER_AGENT],'是否显示版本号')  //获取客户端操作系统和版本号 * * @author wuzhc 2016-01-23 */class clientUtil {    /**     * 各类主流游览器正则 * @var array     * @author wuzhc 2016-01-22     */    protected static $browsers = array(        'Edge'            => 'Edge',        'IE'              => 'MSIE|IEMobile|MSIEMobile|Trident/[.0-9]+',        'Chrome'          => '(?:\bCrMo\b|CriOS|Android)?.*Chrome/[.0-9]* (Mobile)?',        'Opera'           => 'Opera.*Mini|Opera.*Mobi|Android.*Opera|Mobile.*OPR/[0-9.]+|Coast/[0-9.]+',        'Firefox'         => 'fennec|firefox.*maemo|(Mobile|Tablet).*Firefox|Firefox.*Mobile',        'Safari'          => 'Version.*Mobile.*Safari|Safari.*Mobile|MobileSafari',        'UCBrowser'       => 'UC.*Browser|UCWEB', //UC游览器        'QQBrowser'       => 'MQQBrowser|TencentTraveler', //QQ游览器        'The world'       => 'The world', //世界之窗游览器        'Maxthon'         => 'Maxthon', //遨游游览器        'baiduboxapp'     => 'baiduboxapp',        'baidubrowser'    => 'baidubrowser',        'NokiaBrowser'    => 'Nokia',    );    /**     * 操作系统正则     * note:移动设备的系统需优先匹配     *      故正则需要放在电脑系统前面     * @var array     * @author wuzhc 2016-01-23     */    protected static $platforms = array(        'iOS'               => '\biPhone.*Mobile|\biPod|\biPad',        'Windows Mobile OS' => 'Windows CE.*(PPC|Smartphone|Mobile|[0-9]{3}x[0-9]{3})|Window Mobile|Windows Phone [0-9.]+|WCE;',        'Windows Phone OS'  => 'Windows Phone 10.0|Windows Phone 8.1|Windows Phone 8.0|Windows Phone OS|XBLWP7|ZuneWP7|Windows NT 6.[23]; ARM;',        'Android'           => 'Android',        'BlackBerry OS'     => 'blackberry|\bBB10\b|rim tablet os', //黑莓        'SymbianOS'         => 'Symbian|SymbOS|Series60|Series40|SYB-[0-9]+|\bS60\b', //塞班        'webOS'             => 'webOS|hpwOS',        'MicroMessenger'    => 'MicroMessenger', //微信        'Windows'           => 'Windows',        'Windows NT'        => 'Windows NT',        'Mac OS X'          => 'Mac OS X',        'Ubuntu'            => 'Ubuntu',        'Linux'             => 'Linux',        'Chrome OS'         => 'CrOS',    );    /**     * 版本号匹配正则(游览器 + 操作系统)     * @var array     * @author wuzhc 2016-01-22     */    protected static $versionRegexs = array(        // Browser        'Maxthon'       => 'Maxthon [VER]', //遨游        'Chrome'        => array('Chrome/[VER]', 'CriOS/[VER]', 'CrMo/[VER]'), //谷歌        'Firefox'       => 'Firefox/[VER]', //火狐        'Fennec'        => 'Fennec/[VER]', //火狐        'IE'            => array('IEMobile/[VER];', 'IEMobile [VER]', 'MSIE [VER];', 'rv:[VER]'),        'Opera'         => array('OPR/[VER]', 'Opera Mini/[VER]', 'Version/[VER]', 'Opera [VER]'),        'UC Browser'    => 'UC Browser[VER]', //UC        'QQBrowser'     => array('MQQBrowser/[VER]','TencentTraveler/[VER]'), //QQ        'MicroMessenger'=> 'MicroMessenger/[VER]', //微信        'baiduboxapp'   => 'baiduboxapp/[VER]', //百度盒子        'baidubrowser'  => 'baidubrowser/[VER]', //百度        'Safari'        => array('Version/[VER]', 'Safari/[VER]' ), //Mac OS X中的浏览器        'NokiaBrowser'  => 'NokiaBrowser/[VER]', //诺基亚        // OS        'iOS'              => '\bi?OS\b [VER][ ;]{1}',        'BlackBerry'       => array('BlackBerry[\w]+/[VER]', 'BlackBerry.*Version/[VER]', 'Version/[VER]'), //黑莓手机        'Windows Phone OS' => array( 'Windows Phone OS [VER]', 'Windows Phone [VER]'),        'Windows Phone'    => 'Windows Phone [VER]',        'Windows NT'       => 'Windows NT [VER]',        'Windows'          => 'Windows NT [VER]',        'SymbianOS'        => array('SymbianOS/[VER]', 'Symbian/[VER]'), //塞班系统        'webOS'            => array('webOS/[VER]', 'hpwOS/[VER];'), //LG        'Mac OS X'         => 'MAC OS X [VER]', //苹果系统        'BlackBerry OS'    => array('BlackBerry[\w]+/[VER]', 'BlackBerry.*Version/[VER]', 'Version/[VER]'),        'Android'          => 'Android [VER]',        'Chrome OS'        => 'CrOS x86_64 [VER]',    );    /**     * 获取客户端游览器     * @param $userAgent $_SERVER['HTTP_USER_AGENT']     * @param bool $isReTurnVersion //是否一起返回版本号     * @return string (类型 + 版本号)     * @author wuzhc 2016-01-23     */    public static function getBrowser($userAgent,$isReTurnVersion = false) {        if(empty($userAgent)) {            return '';        }        $clientBrowser = '';        foreach((array)self::$browsers as $key => $browser) {            if(self::match($browser,$userAgent)) {                $clientBrowser = $key;                break;            }        }        if($isReTurnVersion && $clientBrowser) {            $clientBrowser .= ' '.self::getVersion($clientBrowser,$userAgent);        }        return $clientBrowser;    }    /**     * 获取客户端操作系统     * @param $userAgent     * @param bool $isReTurnVersion //是否一起返回版本号     * @return string     * @author wuzhc 2016-01-23     */    public static function getPlatForm($userAgent,$isReTurnVersion = false) {        if(empty($userAgent)) {            return '';        }        $clientPlatform = '';        foreach((array)self::$platforms as $key => $platform) {            if(self::match($platform,$userAgent)) {                $clientPlatform = $key;                break;            }        }        if($isReTurnVersion && $clientPlatform) {            $clientPlatform .= ' '.self::getVersion($clientPlatform,$userAgent);        }        return $clientPlatform;    }    /**     * 查询版本号     * @param $propertyName (操作系统名称和或游览器名称)     * @see self::$versionRegexs     * @param $userAgent     * @return string     * @author wuzhc 2016-01-22     */    public static function getVersion($propertyName,$userAgent) {        $verRegex = array_key_exists($propertyName,self::$versionRegexs)            ? self::$versionRegexs[$propertyName] : null;        if(!$verRegex) {            return '';        } else {            $verRegex = (array)$verRegex;        }        $match = self::matchVersion($verRegex,$userAgent); //开始匹配        if($match && stripos($propertyName,'window') !== false) { //windown系统版本号需要转换            return self::getWinVersion($match);        } else {            return str_replace('_','.',$match);        }    }    /**     * 根据匹配结果转换window系统版本号     * @param $match     * @return string     * @author wuzhc 2016-01-22     */    protected static function getWinVersion($match) {        if($match == '6.0') {            return 'Vista';        }        else if($match == '6.1') {            return '7';        }        else if($match == '6.2') {            return '8';        }        else if($match == '5.1') {            return 'XP';        }    }    /**     * 正则匹配     * @param array $regex     * @param $userAgent     * @return string     * @author wuzhc 2016-01-22     */    protected static function match($regex,$userAgent) {        return (bool)preg_match(sprintf('#%s#is',$regex),$userAgent,$matches);    }    /**     * 版本号正则匹配     * @param array $regexs     * @param $userAgent     * @return string     * @author wuzhc 2016-01-22     */    protected static function matchVersion($regexs,$userAgent) {        foreach((array)$regexs as $regex) {            $regex = str_replace('[VER]','([\w\.]+)', $regex);            $match = (bool)preg_match(sprintf('#%s#is',$regex),$userAgent,$matches);            if($match) {                return $matches[1];            }        }        return '';    }}
Copy after login



 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? Apr 01, 2025 pm 03:09 PM

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...

See all articles