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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

Alipay PHP...

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,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

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