PHP获取客户端操作系统,游览器类型及版本号
<?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 ''; }}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

Alipay PHP...

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

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,

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

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�...

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

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...
