浏览器检测:从用户代理确定浏览器和操作系统
2017-10-16 00:00
435 查看
<?php /** * @version $1.1$ * @package dinke.net * @copyright © 2008 Dinke.net * @author Dragan Dinic <dragan@dinke.net> */ /** * Browser Detection class * contains common static method for * getting browser version and os * * It supports most popular browsers (IE, FF, Safari, Opera, Chrome ...) * As well as some not so popular (lynx etc.) * It doesn't recognize bots (like google, yahooo etc) * * usage * <code> * $browser = Browser_Detection::get_browser($_SERVER['HTTP_USER_AGENT']); * $os = Browser_Detection::get_os($_SERVER['HTTP_USER_AGENT']); * </code> * @access public */ class Browser_Detection { /** * Get browsername and version * @param string user agent * @return string browser name and version or false if unrecognized * @static * @access public */ function get_browser($useragent) { //check for most popular browsers first //unfortunately that's ie. We also ignore opera and netscape 8 //because they sometimes send msie agent if(strpos($useragent,"MSIE") !== false && strpos($useragent,"Opera") === false && strpos($useragent,"Netscape") === false) { //deal with IE $found = preg_match("/MSIE ([0-9]{1}\.[0-9]{1,2})/",$useragent, $mathes); if($found) { return "Internet Explorer " . $mathes[1]; } } elseif(strpos($useragent,"Gecko")) { //deal with Gecko based //if firefox $found = preg_match("/Firefox\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/",$useragent,$mathes); if($found) { return "Mozilla Firefox " . $mathes[1]; } //if Netscape (based on gecko) $found = preg_match("/Netscape\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/",$useragent,$mathes); if($found) { return "Netscape " . $mathes[1]; } //check chrome before safari because chrome agent contains both $found = preg_match("/Chrome\/([^\s]+)/",$useragent, $mathes); if($found) { return "Google Chrome " . $mathes[1]; } //if Safari (based on gecko) $found = preg_match("/Safari\/([0-9]{2,3}(\.[0-9])?)/",$useragent, $mathes); if($found) { return "Safari " . $mathes[1]; } //if Galeon (based on gecko) $found = preg_match("/Galeon\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/",$useragent,$mathes); if($found) { return "Galeon " . $mathes[1]; } //if Konqueror (based on gecko) $found = preg_match("/Konqueror\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/",$useragent,$mathes); if($found) { return "Konqueror " . $mathes[1]; } //no specific Gecko found //return generic Gecko return "Gecko based"; } elseif(strpos($useragent,"Opera") !== false) { //deal with Opera $found = preg_match("/Opera[\/ ]([0-9]{1}\.[0-9]{1}([0-9])?)/",$useragent,$mathes); if($found) { return "Opera " . $mathes[1]; } } elseif (strpos($useragent,"Lynx") !== false) { //deal with Lynx $found = preg_match("/Lynx\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/",$useragent,$mathes); if($found) { return "Lynx " . $mathes[1]; 7fe0 } } elseif (strpos($useragent,"Netscape") !== false) { //NN8 with IE string $found = preg_match("/Netscape\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/",$useragent,$mathes); if($found) { return "Netscape " . $mathes[1]; } } else { //unrecognized, this should be less than 1% of browsers (not counting bots like google etc)! return false; } } /** * Get browsername and version * @param string user agent * @return string os name and version or false in unrecognized os * @static * @access public */ function get_os($useragent) { $useragent = strtolower($useragent); //check for (aaargh) most popular first //winxp if(strpos("$useragent","windows nt 5.1") !== false) { return "Windows XP"; } elseif (strpos("$useragent","windows nt 6.0") !== false) { return "Windows Vista"; } elseif (strpos("$useragent","windows 98") !== false) { return "Windows 98"; } elseif (strpos("$useragent","windows nt 5.0") !== false) { return "Windows 2000"; } elseif (strpos("$useragent","windows nt 5.2") !== false) { return "Windows 2003 server"; } elseif (strpos("$useragent","windows nt 6.0") !== false) { return "Windows Vista"; } elseif (strpos("$useragent","windows nt") !== false) { return "Windows NT"; } elseif (strpos("$useragent","win 9x 4.90") !== false && strpos("$useragent","win me")) { return "Windows ME"; } elseif (strpos("$useragent","win ce") !== false) { return "Windows CE"; } elseif (strpos("$useragent","win 9x 4.90") !== false) { return "Windows ME"; } elseif (strpos("$useragent","iphone") !== false) { return "iPhone"; } elseif (strpos("$useragent","mac os x") !== false) { return "Mac OS X"; } elseif (strpos("$useragent","macintosh") !== false) { return "Macintosh"; } elseif (strpos("$useragent","linux") !== false) { return "Linux"; } elseif (strpos("$useragent","freebsd") !== false) { return "Free BSD"; } elseif (strpos("$useragent","symbian") !== false) { return "Symbian"; } else { return false; } } } ?>
使用方式
<?php require_once("browser_detection.php"); $browser = Browser_Detection::get_browser($_SERVER['HTTP_USER_AGENT']); if($browser !== false) { echo "Your browser is $browser <br />"; } $os = Browser_Detection::get_os($_SERVER['HTTP_USER_AGENT']); if($os !== false) { echo "Your OS is $os <br />"; } ?>
相关文章推荐
- userAgent,JS用户代理检测——判断浏览器内核、浏览器、浏览器平台、windows操作系统版本、移动设备、游戏系统
- userAgent,JS用户代理检测——判断浏览器内核、浏览器、浏览器平台、windows操作系统版本、移动设备、游戏系统
- 如何使用ActionScript来检测用户的操作系统种类及浏览器类型
- 【javascript】浏览器用户代理检测脚本实现
- userAgent,JS这么屌的用户代理,你造吗?——判断浏览器内核、浏览器、浏览器平台、windows操作系统版本、移动设备、游戏系统
- javascript浏览器用户代理检测脚本实现方法
- Internet Explorer® 8 之后,您可使用自动代理和自动检测功能,全局更改一些浏览器设置,而无需更改每个用户的计算机
- 浏览器用户代理检测
- 认识Javascript中的navigator对象以及检测浏览器类型和版本,获取浏览器版本号,检测客户端操作系统
- JS获取浏览器UA(User Agent 用户代理)方法
- Javascript检测浏览器类型以及操作系统
- JS笔记:检测客户端(引擎、浏览器、平台、操作系统、移动设备)
- adblockTester通过js检测用户浏览器是否安装了AdBlock
- Javascript检测客户端分辨率,操作系统,浏览器,屏幕色彩信息
- 第三百二十八节,web爬虫讲解2—urllib库爬虫—状态吗—异常处理—浏览器伪装技术、设置用户代理
- JS检测用户使用哪种浏览器
- 浏览器事件一些js代码,自己备用的。高手不要笑话我。。(跨浏览器基础事件,浏览器检测,判断浏览器的名称、版本号、操作系统)
- DOM(十四):代理检测和事件处理(跨浏览器)
- 浏览器USER-AGENT(用户代理)的介绍
- 浏览器User-Agent(用户代理)的介绍