今天的项目需要根据用户的浏览器类型进行不同的处理,所以稍微研究了一下使用php判断浏览器类型的方法,发现解决这个问题其实很简单,只要熟悉常量$ [''] 用法就行。接下来我们写一个php通过$['']判断浏览器类型的方法。
function my_get_browser(){ if(empty($_server['http_user_agent'])){ return '命令行,机器人来了!'; } if(false!==strpos($_server['http_user_agent'],'msie 9.0')){ return 'internet explorer 9.0'; } if(false!==strpos($_server['http_user_agent'],'msie 8.0')){ return 'internet explorer 8.0'; } if(false!==strpos($_server['http_user_agent'],'msie 7.0')){ return 'internet explorer 7.0'; } if(false!==strpos($_server['http_user_agent'],'msie 6.0')){ return 'internet explorer 6.0'; } if(false!==strpos($_server['http_user_agent'],'firefox')){ return 'firefox'; } if(false!==strpos($_server['http_user_agent'],'chrome')){ return 'chrome'; } if(false!==strpos($_server['http_user_agent'],'safari')){ return 'safari'; } if(false!==strpos($_server['http_user_agent'],'opera')){ return 'opera'; } if(false!==strpos($_server['http_user_agent'],'360se')){ return '360se'; }}
这里主要使用$[''],这个常量用来检查页面的访问者使用的是什么操作系统(包括版本号)、浏览器(包括版本号)和用户个人偏好。用法非常简单。您可以通过在程序中打印出来来理解它。
另外,php还可以使用php系统函数()函数来判断浏览器类型,它会返回用户浏览器的一些性能数据。此功能通过查询用户的 .ini 文件来衡量用户浏览器的性能。如果成功,该函数返回一个包含用户浏览器信息的对象或数组,如果失败,则返回。
语法
(,)
该函数有两个参数,参数含义解释如下:
可选的。指定 http 用户代理的名称。默认值为 $ 的值。您可以通过设置 null 来绕过此参数。
可选的。如果此参数设置为 true,则此函数将返回一个数组而不是一个对象。
对于php通过$['']获取的浏览器信息,我们可以通过下面的例子来了解:
结果将输出:
/4.0
(; msie 6.0; nt 5.1; sv1; .net clr 1.1.4322)
(
[] => ^/.\.0
(; msie 6\.0.*;.* nt 5\.1.*\.net clr.*).*$
[] => /?.0
(; msie 6.0*;* nt 5.1*.net clr*)*
[] => ie 6.0
[] =>
[] => 1
[] => ie
[] => 6.0
[] => 6
[] => 0
[css] => 2
[] => 1
[] => 1
[] => 1
[] => 1
[] => 1
[] => 1
[] => 1
[] => 1
[] => 1
[cdf] => 1
[aol] =>
[测试版] =>
[] =>
[] =>
[] =>
[wap] =>
[ak] =>
[sk] =>
)
但是php获取服务器ie版本,通过php()函数获取客户端浏览器的相关信息是有条件的。如果直接使用,一般会报如下错误:
:() [.get-]: ini 中没有设置......
通过查询php手册,解释是:要使用这个功能,必须在php.ini文件中加一句指向.ini文件的配置。.ini 文件记录了所有现有浏览器的类型和信息。所以你必须下载最新的文件,下载 .ini 并将它放在服务器上的某个位置。()使用时,将获取到的客户端信息与该文件进行比较,如果可以找到,则返回对应的类型。
从以上来看,使用()获取浏览器类型的代价不小!
php 还有一种判断浏览器类型的方法,就是使用国外专家写的开源代码。有个叫国外的网站,里面有关于浏览器的项目,并且不断更新写的类。我看到的最新类可以获取包括,win,mac,os,beos等平台的浏览器信息,功能可以说是非常强大了。下载此文件时,应根据您使用的 php 版本进行选择。该类的具体代码如下:
getbrowser() == browser::browser_firefox && $browser->getversion() >= 2 ) { * echo 'you have firefox version 2 or greater'; * } * * user agents sampled from: http://www.useragentstring.com/ * * this implementation is based on the original work from gary white * http://apptools.com/phptools/browser/ * * updates: * * 2010-08-20 (v1.9): * + added msn explorer browser (legacy) * + added bing/msn robot (thanks rob macdonald) * + added the android platform (platform_android) * + fixed issue with android 1.6/2.2 (thanks tom hirashima) * * 2010-04-27 (v1.8): * + added ipad support * * 2010-03-07 (v1.7): * + *major* rebuild (preg_match and other slow routine removal(s)) * + almost allof gary's original code has been replaced * + large phpunit testing environment created to validate new releases and additions * + added freebsd platform * + added openbsd platform * + added netbsd platform * + added sunos platform * + added opensolaris platform * + added support of the iceweazel browser * + added ischromeframe() call to check if chromeframe is in use * + moved the opera check in front of the firefox check due to legacy opera user agents * + added the __tostring() method (thanks deano) * * 2009-11-15: * + updated the checkes for firefox * + added the nokia platform * + added checks for the nokia brower(s) * * 2009-11-08: * + php 5.3 support * + added support for blackberry os and blackberry browser * + added support for the opera mini browser * + added additional documenation * + added support for isrobot() and ismobile() * + added support for opera version 10 * + added support for deprecated netscape navigator version 9 * + added support for icecat * + added support for shiretoko * * 2010-04-27 (v1.8): * + added ipad support * * 2009-08-18: * + updated to support php 5.3 - removed all deprecated function calls * + updated to remove all double quotes () -- converted to single quotes (') * * 2009-04-27: * + updated the ie check to remove a typo and bug (thanks john) * * 2009-04-22: * + added detection for googlebot * + added detection for the w3c validator. * + added detection for yahoo! slurp * * 2009-03-14: * + added detection for ipods. * + added platform detection for iphones * + added platform detection for ipods * * 2009-02-16: (rick hale) * + added version detection for android phones. * * 2008-12-09: * + removed unused constant * * 2008-11-07: * + added google's chrome to the detection list * + added isbrowser(string) to the list of functions special thanks to *daniel 'mavrick' lang for the function concept (http://mavrick.id.au) * * * gary white noted: since browser detection is so unreliable, i am * no longer maintaining this script. you are free to use and or * modify/update it as you want, however the author assumes no * responsibility for the accuracy of the detected values. * * anyone experienced with gary's script might be interested in these notes: * * added class constants * added detection and version detection for google's chrome * updated the version detection for amaya * updated the version detection for firefox * updated the version detection for lynx * updated the version detection for webtv * updated the version detection for netpositive * updated the version detection for ie * updated the version detection for omniweb * updated the version detection for icab * updated the version detection for safari * updated safari to remove mobile devices (iphone) * added detection for iphone * added detection for robots * added detection for mobile devices * added detection for blackberry * removed netscape checks (matches heavily with firefox & mozilla) * */class browser { private $_agent = ''; private $_browser_name = ''; private $_version = ''; private $_platform = ''; private $_os = ''; private $_is_aol = false; private $_is_mobile = false; private $_is_robot = false; private $_aol_version = ''; const browser_unknown = 'unknown'; const version_unknown = 'unknown'; const browser_opera = 'opera';// http://www.opera.com/ const browser_opera_mini = 'opera mini'; // http://www.opera.com/mini/ const browser_webtv = 'webtv';// http://www.webtv.net/pc/ const browser_ie = 'internet explorer'; // http://www.microsoft.com/ie/ const browser_pocket_ie = 'pocket internet explorer'; // http://en.wikipedia.org/wiki/internet_explorer_mobile const browser_konqueror = 'konqueror';// http://www.konqueror.org/ const browser_icab = 'icab'; // http://www.icab.de/ const browser_omniweb = 'omniweb';// http://www.omnigroup.com/applications/omniweb/ const browser_firebird = 'firebird'; // http://www.ibphoenix.com/ const browser_firefox = 'firefox';// http://www.mozilla.com/en-us/firefox/firefox.html const browser_iceweasel = 'iceweasel';// http://www.geticeweasel.org/ const browser_shiretoko = 'shiretoko';// http://wiki.mozilla.org/projects/shiretoko const browser_mozilla = 'mozilla';// http://www.mozilla.com/en-us/ const browser_amaya = 'amaya';// http://www.w3.org/amaya/ const browser_lynx = 'lynx'; // http://en.wikipedia.org/wiki/lynx const browser_safari = 'safari'; // http://apple.com const browser_iphone = 'iphone'; // http://apple.com const browser_ipod = 'ipod'; // http://apple.com const browser_ipad = 'ipad'; // http://apple.com const browser_chrome = 'chrome'; // http://www.google.com/chrome const browser_android = 'android';// http://www.android.com/ const browser_googlebot = 'googlebot';// http://en.wikipedia.org/wiki/googlebot const browser_slurp = 'yahoo! slurp'; // http://en.wikipedia.org/wiki/yahoo!_slurp const browser_w3cvalidator = 'w3c validator'; // http://validator.w3.org/ const browser_blackberry = 'blackberry'; // http://www.blackberry.com/ const browser_icecat = 'icecat'; // http://en.wikipedia.org/wiki/gnu_icecat const browser_nokia_s60 = 'nokia s60 oss browser';// http://en.wikipedia.org/wiki/web_browser_for_s60 const browser_nokia = 'nokia browser';// * all other wap-based browsers on the nokia platform const browser_msn = 'msn browser';// http://explorer.msn.com/ const browser_msnbot = 'msn bot'; // http://search.msn.com/msnbot.htm // http://en.wikipedia.org/wiki/msnbot (used for bing as well) const browser_netscape_navigator = 'netscape navigator'; // http://browser.netscape.com/ (deprecated) const browser_galeon = 'galeon'; // http://galeon.sourceforge.net/ (deprecated) const browser_netpositive = 'netpositive';// http://en.wikipedia.org/wiki/netpositive (deprecated) const browser_phoenix = 'phoenix';// http://en.wikipedia.org/wiki/history_of_mozilla_firefox (deprecated) const platform_unknown = 'unknown'; const platform_windows = 'windows'; const platform_windows_ce = 'windows ce'; const platform_apple = 'apple'; const platform_linux = 'linux'; const platform_os2 = 'os/2'; const platform_beos = 'beos'; const platform_iphone = 'iphone'; const platform_ipod = 'ipod'; const platform_ipad = 'ipad'; const platform_blackberry = 'blackberry'; const platform_nokia = 'nokia'; const platform_freebsd = 'freebsd'; const platform_openbsd = 'openbsd'; const platform_netbsd = 'netbsd'; const platform_sunos = 'sunos'; const platform_opensolaris = 'opensolaris'; const platform_android = 'android'; const operating_system_unknown = 'unknown'; public function browser($useragent=) { $this->reset(); if( $useragent != ) { $this->setuseragent($useragent); } else { $this->determine(); } } /** * reset all properties */ public function reset() { $this->_agent = isset($_server['http_user_agent']) ? $_server['http_user_agent'] : ; $this->_browser_name = self::browser_unknown; $this->_version = self::version_unknown; $this->_platform = self::platform_unknown; $this->_os = self::operating_system_unknown; $this->_is_aol = false; $this->_is_mobile = false; $this->_is_robot = false; $this->_aol_version = self::version_unknown; } /** * check to see if the specific browser is valid * @param string $browsername * @return true if the browser is the specified browser */ function isbrowser($browsername) { return( 0 == strcasecmp($this->_browser_name, trim($browsername))); } /** * the name of the browser. all return types are from the class contants * @return string name of the browser */ public function getbrowser() { return $this->_browser_name; } /** * set the name of the browser * @param $browser the name of the browser */ public function setbrowser($browser) { return $this->_browser_name = $browser; } /** * the name of the platform. all return types are from the class contants * @return string name of the browser */ public function getplatform() { return $this->_platform; } /** * set the name of the platform * @param $platform the name of the platform */ public function setplatform($platform) { return $this->_platform = $platform; } /** * the version of the browser. * @return string version of the browser (will only contain alpha-numeric characters and a period) */ public function getversion() { return $this->_version; } /** * set the version of the browser * @param $version the version of the browser */ public function setversion($version) { $this->_version = preg_replace('/[^0-9,.,a-z,a-z-]/','',$version); } /** * the version of aol. * @return string version of aol (will only contain alpha-numeric characters and a period) */ public function getaolversion() { return $this->_aol_version; } /** * set the version of aol * @param $version the version of aol */ public function setaolversion($version) { $this->_aol_version = preg_replace('/[^0-9,.,a-z,a-z]/','',$version); } /** * is the browser from aol? * @return boolean true if the browser is from aol otherwise false */ public function isaol() { return $this->_is_aol; } /** * is the browser from a mobile device? * @return boolean true if the browser is from a mobile device otherwise false */ public function ismobile() { return $this->_is_mobile; } /** * is the browser from a robot (ex slurp,googlebot)? * @return boolean true if the browser is from a robot otherwise false */ public function isrobot() { return $this->_is_robot; } /** * set the browser to be from aol * @param $isaol */ public function setaol($isaol) { $this->_is_aol = $isaol; } /** * set the browser to be mobile * @param boolean $value is the browser a mobile brower or not */ protected function setmobile($value=true) { $this->_is_mobile = $value; } /** * set the browser to be a robot * @param boolean $value is the browser a robot or not */ protected function setrobot($value=true) { $this->_is_robot = $value; } /** * get the user agent value in use to determine the browser * @return string the user agent from the http header */ public function getuseragent() { return $this->_agent; } /** * set the user agent value (the construction will use the http header value - this will overwrite it) * @param $agent_string the value for the user agent */ public function setuseragent($agent_string) { $this->reset(); $this->_agent = $agent_string; $this->determine(); } /** * used to determine if the browser is actually chromeframe * @since 1.7 * @return boolean true if the browser is using chromeframe */ public function ischromeframe() { return( strpos($this->_agent,chromeframe) !== false ); } /** * returns a formatted string with a summary of the details of the browser. * @return string formatted string with a summary of the browser */ public function __tostring() { return browser name:{$this->getbrowser()}
\n . browser version:{$this->getversion()}
\n . browser user agent string:{$this->getuseragent()}
\n . platform:{$this->getplatform()}
; } /** * protected routine to calculate and determine what the browser is in use (including platform) */ protected function determine() { $this->checkplatform(); $this->checkbrowsers(); $this->checkforaol(); } /** * protected routine to determine the browser type * @return boolean true if the browser was detected otherwise false */ protected function checkbrowsers() { return ( // well-known, well-used // special notes: // (1) opera must be checked before firefox due to the odd // user agents used in some older versions of opera // (2) webtv is strapped onto internet explorer so we must // check for webtv before ie // (3) (deprecated) galeon is based on firefox and needs to be // tested before firefox is tested // (4) omniweb is based on safari so omniweb check must occur // before safari // (5) netscape 9+ is based on firefox so netscape checks // before firefox are necessary $this->checkbrowserwebtv() || $this->checkbrowserinternetexplorer() || $this->checkbrowseropera() || $this->checkbrowsergaleon() || $this->checkbrowsernetscapenavigator9plus() || $this->checkbrowserfirefox() || $this->checkbrowserchrome() || $this->checkbrowseromniweb() || // common mobile $this->checkbrowserandroid() || $this->checkbrowseripad() || $this->checkbrowseripod() || $this->checkbrowseriphone() || $this->checkbrowserblackberry() || $this->checkbrowsernokia() || // common bots $this->checkbrowsergooglebot() || $this->checkbrowsermsnbot() || $this->checkbrowserslurp() || // webkit base check (post mobile and others) $this->checkbrowsersafari() || // everyone else $this->checkbrowsernetpositive() || $this->checkbrowserfirebird() || $this->checkbrowserkonqueror() || $this->checkbrowsericab() || $this->checkbrowserphoenix() || $this->checkbrowseramaya() || $this->checkbrowserlynx() || $this->checkbrowsershiretoko() || $this->checkbrowsericecat() || $this->checkbrowserw3cvalidator() || $this->checkbrowsermozilla() /* mozilla is such an open standard that you must check it last */ ); } /** * determine if the user is using a blackberry (last updated 1.7) * @return boolean true if the browser is the blackberry browser otherwise false */ protected function checkbrowserblackberry() { if( stripos($this->_agent,'blackberry') !== false ) { $aresult = explode(/,stristr($this->_agent,blackberry)); $aversion = explode(' ',$aresult[1]); $this->setversion($aversion[0]); $this->_browser_name = self::browser_blackberry; $this->setmobile(true); return true; } return false; } /** * determine if the user is using an aol user agent (last updated 1.7) * @return boolean true if the browser is from aol otherwise false */ protected function checkforaol() { $this->setaol(false); $this->setaolversion(self::version_unknown); if( stripos($this->_agent,'aol') !== false ) { $aversion = explode(' ',stristr($this->_agent, 'aol')); $this->setaol(true); $this->setaolversion(preg_replace('/[^0-9\.a-z]/i', '', $aversion[1])); return true; } return false; } /** * determine if the browser is the googlebot or not (last updated 1.7) * @return boolean true if the browser is the googletbot otherwise false */ protected function checkbrowsergooglebot() { if( stripos($this->_agent,'googlebot') !== false ) { $aresult = explode('/',stristr($this->_agent,'googlebot')); $aversion = explode(' ',$aresult[1]); $this->setversion(str_replace(';','',$aversion[0])); $this->_browser_name = self::browser_googlebot; $this->setrobot(true); return true; } return false; } /** * determine if the browser is the msnbot or not (last updated 1.9) * @return boolean true if the browser is the msnbot otherwise false */ protected function checkbrowsermsnbot() { if( stripos($this->_agent,msnbot) !== false ) { $aresult = explode(/,stristr($this->_agent,msnbot)); $aversion = explode( ,$aresult[1]); $this->setversion(str_replace(;,,$aversion[0])); $this->_browser_name = self::browser_msnbot; $this->setrobot(true); return true; } return false; } /** * determine if the browser is the w3c validator or not (last updated 1.7) * @return boolean true if the browser is the w3c validator otherwise false */ protected function checkbrowserw3cvalidator() { if( stripos($this->_agent,'w3c-checklink') !== false ) { $aresult = explode('/',stristr($this->_agent,'w3c-checklink')); $aversion = explode(' ',$aresult[1]); $this->setversion($aversion[0]); $this->_browser_name = self::browser_w3cvalidator; return true; } else if( stripos($this->_agent,'w3c_validator') !== false ) { // some of the validator versions do not delineate w/ a slash - add it back in $ua = str_replace(w3c_validator , w3c_validator/, $this->_agent); $aresult = explode('/',stristr($ua,'w3c_validator')); $aversion = explode(' ',$aresult[1]); $this->setversion($aversion[0]); $this->_browser_name = self::browser_w3cvalidator; return true; } return false; } /** * determine if the browser is the yahoo! slurp robot or not (last updated 1.7) * @return boolean true if the browser is the yahoo! slurp robot otherwise false */ protected function checkbrowserslurp() { if( stripos($this->_agent,'slurp') !== false ) { $aresult = explode('/',stristr($this->_agent,'slurp')); $aversion = explode(' ',$aresult[1]); $this->setversion($aversion[0]); $this->_browser_name = self::browser_slurp; $this->setrobot(true); $this->setmobile(false); return true; } return false; } /** * determine if the browser is internet explorer or not (last updated 1.7) * @return boolean true if the browser is internet explorer otherwise false */ protected function checkbrowserinternetexplorer() { // test for v1 - v1.5 ie if( stripos($this->_agent,'microsoft internet explorer') !== false ) { $this->setbrowser(self::browser_ie); $this->setversion('1.0'); $aresult = stristr($this->_agent, '/'); if( preg_match('/308|425|426|474|0b1/i', $aresult) ) { $this->setversion('1.5'); } return true; } // test for versions > 1.5 else if( stripos($this->_agent,'msie') !== false && stripos($this->_agent,'opera') === false ) { // see if the browser is the odd msn explorer if( stripos($this->_agent,'msnb') !== false ) { $aresult = explode(' ',stristr(str_replace(';','; ',$this->_agent),'msn')); $this->setbrowser( self::browser_msn ); $this->setversion(str_replace(array('(',')',';'),'',$aresult[1])); return true; } $aresult = explode(' ',stristr(str_replace(';','; ',$this->_agent),'msie')); $this->setbrowser( self::browser_ie ); $this->setversion(str_replace(array('(',')',';'),'',$aresult[1])); return true; } // test for pocket ie else if( stripos($this->_agent,'mspie') !== false || stripos($this->_agent,'pocket') !== false ) { $aresult = explode(' ',stristr($this->_agent,'mspie')); $this->setplatform( self::platform_windows_ce ); $this->setbrowser( self::browser_pocket_ie ); $this->setmobile(true); if( stripos($this->_agent,'mspie') !== false ) { $this->setversion($aresult[1]); } else { $aversion = explode('/',$this->_agent); $this->setversion($aversion[1]); } return true; } return false; } /** * determine if the browser is opera or not (last updated 1.7) * @return boolean true if the browser is opera otherwise false */ protected function checkbrowseropera() { if( stripos($this->_agent,'opera mini') !== false ) { $resultant = stristr($this->_agent, 'opera mini'); if( preg_match('/\//',$resultant) ) { $aresult = explode('/',$resultant); $aversion = explode(' ',$aresult[1]); $this->setversion($aversion[0]); } else { $aversion = explode(' ',stristr($resultant,'opera mini')); $this->setversion($aversion[1]); } $this->_browser_name = self::browser_opera_mini; $this->setmobile(true); return true; } else if( stripos($this->_agent,'opera') !== false ) { $resultant = stristr($this->_agent, 'opera'); if( preg_match('/version\/(10.*)$/',$resultant,$matches) ) { $this->setversion($matches[1]); } else if( preg_match('/\//',$resultant) ) { $aresult = explode('/',str_replace((, ,$resultant)); $aversion = explode(' ',$aresult[1]); $this->setversion($aversion[0]); } else { $aversion = explode(' ',stristr($resultant,'opera')); $this->setversion(isset($aversion[1])?$aversion[1]:); } $this->_browser_name = self::browser_opera; return true; } return false; } /** * determine if the browser is chrome or not (last updated 1.7) * @return boolean true if the browser is chrome otherwise false */ protected function checkbrowserchrome() { if( stripos($this->_agent,'chrome') !== false ) { $aresult = explode('/',stristr($this->_agent,'chrome')); $aversion = explode(' ',$aresult[1]); $this->setversion($aversion[0]); $this->setbrowser(self::browser_chrome); return true; } return false; } /** * determine if the browser is webtv or not (last updated 1.7) * @return boolean true if the browser is webtv otherwise false */ protected function checkbrowserwebtv() { if( stripos($this->_agent,'webtv') !== false ) { $aresult = explode('/',stristr($this->_agent,'webtv')); $aversion = explode(' ',$aresult[1]); $this->setversion($aversion[0]); $this->setbrowser(self::browser_webtv); return true; } return false; } /** * determine if the browser is netpositive or not (last updated 1.7) * @return boolean true if the browser is netpositive otherwise false */ protected function checkbrowsernetpositive() { if( stripos($this->_agent,'netpositive') !== false ) { $aresult = explode('/',stristr($this->_agent,'netpositive')); $aversion = explode(' ',$aresult[1]); $this->setversion(str_replace(array('(',')',';'),'',$aversion[0])); $this->setbrowser(self::browser_netpositive); return true; } return false; } /** * determine if the browser is galeon or not (last updated 1.7) * @return boolean true if the browser is galeon otherwise false */ protected function checkbrowsergaleon() { if( stripos($this->_agent,'galeon') !== false ) { $aresult = explode(' ',stristr($this->_agent,'galeon')); $aversion = explode('/',$aresult[0]); $this->setversion($aversion[1]); $this->setbrowser(self::browser_galeon); return true; } return false; } /** * determine if the browser is konqueror or not (last updated 1.7) * @return boolean true if the browser is konqueror otherwise false */ protected function checkbrowserkonqueror() { if( stripos($this->_agent,'konqueror') !== false ) { $aresult = explode(' ',stristr($this->_agent,'konqueror')); $aversion = explode('/',$aresult[0]); $this->setversion($aversion[1]); $this->setbrowser(self::browser_konqueror); return true; } return false; } /** * determine if the browser is icab or not (last updated 1.7) * @return boolean true if the browser is icab otherwise false */ protected function checkbrowsericab() { if( stripos($this->_agent,'icab') !== false ) { $aversion = explode(' ',stristr(str_replace('/',' ',$this->_agent),'icab')); $this->setversion($aversion[1]); $this->setbrowser(self::browser_icab); return true; } return false; } /** * determine if the browser is omniweb or not (last updated 1.7) * @return boolean true if the browser is omniweb otherwise false */ protected function checkbrowseromniweb() { if( stripos($this->_agent,'omniweb') !== false ) { $aresult = explode('/',stristr($this->_agent,'omniweb')); $aversion = explode(' ',isset($aresult[1])?$aresult[1]:); $this->setversion($aversion[0]); $this->setbrowser(self::browser_omniweb); return true; } return false; } /** * determine if the browser is phoenix or not (last updated 1.7) * @return boolean true if the browser is phoenix otherwise false */ protected function checkbrowserphoenix() { if( stripos($this->_agent,'phoenix') !== false ) { $aversion = explode('/',stristr($this->_agent,'phoenix')); $this->setversion($aversion[1]); $this->setbrowser(self::browser_phoenix); return true; } return false; } /** * determine if the browser is firebird or not (last updated 1.7) * @return boolean true if the browser is firebird otherwise false */ protected function checkbrowserfirebird() { if( stripos($this->_agent,'firebird') !== false ) { $aversion = explode('/',stristr($this->_agent,'firebird')); $this->setversion($aversion[1]); $this->setbrowser(self::browser_firebird); return true; } return false; } /** * determine if the browser is netscape navigator 9+ or not (last updated 1.7) * note: (http://browser.netscape.com/ - official support ended on march 1st, 2008) * @return boolean true if the browser is netscape navigator 9+ otherwise false */ protected function checkbrowsernetscapenavigator9plus() { if( stripos($this->_agent,'firefox') !== false && preg_match('/navigator\/([^ ]*)/i',$this->_agent,$matches) ) { $this->setversion($matches[1]); $this->setbrowser(self::browser_netscape_navigator); return true; } else if( stripos($this->_agent,'firefox') === false && preg_match('/netscape6?\/([^ ]*)/i',$this->_agent,$matches) ) { $this->setversion($matches[1]); $this->setbrowser(self::browser_netscape_navigator); return true; } return false; } /** * determine if the browser is shiretoko or not (https://wiki.mozilla.org/projects/shiretoko) (last updated 1.7) * @return boolean true if the browser is shiretoko otherwise false */ protected function checkbrowsershiretoko() { if( stripos($this->_agent,'mozilla') !== false && preg_match('/shiretoko\/([^ ]*)/i',$this->_agent,$matches) ) { $this->setversion($matches[1]); $this->setbrowser(self::browser_shiretoko); return true; } return false; } /** * determine if the browser is ice cat or not (http://en.wikipedia.org/wiki/gnu_icecat) (last updated 1.7) * @return boolean true if the browser is ice cat otherwise false */ protected function checkbrowsericecat() { if( stripos($this->_agent,'mozilla') !== false && preg_match('/icecat\/([^ ]*)/i',$this->_agent,$matches) ) { $this->setversion($matches[1]); $this->setbrowser(self::browser_icecat); return true; } return false; } /** * determine if the browser is nokia or not (last updated 1.7) * @return boolean true if the browser is nokia otherwise false */ protected function checkbrowsernokia() { if( preg_match(/nokia([^\/]+)\/([^ sp]+)/i,$this->_agent,$matches) ) { $this->setversion($matches[2]); if( stripos($this->_agent,'series60') !== false || strpos($this->_agent,'s60') !== false ) { $this->setbrowser(self::browser_nokia_s60); } else { $this->setbrowser( self::browser_nokia ); } $this->setmobile(true); return true; } return false; } /** * determine if the browser is firefox or not (last updated 1.7) * @return boolean true if the browser is firefox otherwise false */ protected function checkbrowserfirefox() { if( stripos($this->_agent,'safari') === false ) { if( preg_match(/firefox[\/ \(]([^ ;\)]+)/i,$this->_agent,$matches) ) { $this->setversion($matches[1]); $this->setbrowser(self::browser_firefox); return true; } else if( preg_match(/firefox$/i,$this->_agent,$matches) ) { $this->setversion(); $this->setbrowser(self::browser_firefox); return true; } } return false; } /** * determine if the browser is firefox or not (last updated 1.7) * @return boolean true if the browser is firefox otherwise false */ protected function checkbrowsericeweasel() { if( stripos($this->_agent,'iceweasel') !== false ) { $aresult = explode('/',stristr($this->_agent,'iceweasel')); $aversion = explode(' ',$aresult[1]); $this->setversion($aversion[0]); $this->setbrowser(self::browser_iceweasel); return true; } return false; } /** * determine if the browser is mozilla or not (last updated 1.7) * @return boolean true if the browser is mozilla otherwise false */ protected function checkbrowsermozilla() { if( stripos($this->_agent,'mozilla') !== false && preg_match('/rv:[0-9].[0-9][a-b]?/i',$this->_agent) && stripos($this->_agent,'netscape') === false) { $aversion = explode(' ',stristr($this->_agent,'rv:')); preg_match('/rv:[0-9].[0-9][a-b]?/i',$this->_agent,$aversion); $this->setversion(str_replace('rv:','',$aversion[0])); $this->setbrowser(self::browser_mozilla); return true; } else if( stripos($this->_agent,'mozilla') !== false && preg_match('/rv:[0-9]\.[0-9]/i',$this->_agent) && stripos($this->_agent,'netscape') === false ) { $aversion = explode('',stristr($this->_agent,'rv:')); $this->setversion(str_replace('rv:','',$aversion[0])); $this->setbrowser(self::browser_mozilla); return true; } else if( stripos($this->_agent,'mozilla') !== false && preg_match('/mozilla\/([^ ]*)/i',$this->_agent,$matches) && stripos($this->_agent,'netscape') === false ) { $this->setversion($matches[1]); $this->setbrowser(self::browser_mozilla); return true; } return false; } /** * determine if the browser is lynx or not (last updated 1.7) * @return boolean true if the browser is lynx otherwise false */ protected function checkbrowserlynx() { if( stripos($this->_agent,'lynx') !== false ) { $aresult = explode('/',stristr($this->_agent,'lynx')); $aversion = explode(' ',(isset($aresult[1])?$aresult[1]:)); $this->setversion($aversion[0]); $this->setbrowser(self::browser_lynx); return true; } return false; } /** * determine if the browser is amaya or not (last updated 1.7) * @return boolean true if the browser is amaya otherwise false */ protected function checkbrowseramaya() { if( stripos($this->_agent,'amaya') !== false ) { $aresult = explode('/',stristr($this->_agent,'amaya')); $aversion = explode(' ',$aresult[1]); $this->setversion($aversion[0]); $this->setbrowser(self::browser_amaya); return true; } return false; } /** * determine if the browser is safari or not (last updated 1.7) * @return boolean true if the browser is safari otherwise false */ protected function checkbrowsersafari() { if( stripos($this->_agent,'safari') !== false && stripos($this->_agent,'iphone') === false && stripos($this->_agent,'ipod') === false ) { $aresult = explode('/',stristr($this->_agent,'version')); if( isset($aresult[1]) ) { $aversion = explode(' ',$aresult[1]); $this->setversion($aversion[0]); } else $this->setversion(self::version_unknown); $this->setbrowser(self::browser_safari); return true; } return false; } /** * determine if the browser is iphone or not (last updated 1.7) * @return boolean true if the browser is iphone otherwise false */ protected function checkbrowseriphone() { if( stripos($this->_agent,'iphone') !== false ) { $aresult = explode('/',stristr($this->_agent,'version')); if( isset($aresult[1]) ) { $aversion = explode(' ',$aresult[1]); $this->setversion($aversion[0]); } else $this->setversion(self::version_unknown); $this->setmobile(true); $this->setbrowser(self::browser_iphone); return true; } return false; } /** * determine if the browser is ipod or not (last updated 1.7) * @return boolean true if the browser is ipod otherwise false */ protected function checkbrowseripad() { if( stripos($this->_agent,'ipad') !== false ) { $aresult = explode('/',stristr($this->_agent,'version')); if( isset($aresult[1]) ) { $aversion = explode(' ',$aresult[1]); $this->setversion($aversion[0]); } else $this->setversion(self::version_unknown); $this->setmobile(true); $this->setbrowser(self::browser_ipad); return true; } return false; } /** * determine if the browser is ipod or not (last updated 1.7) * @return boolean true if the browser is ipod otherwise false */ protected function checkbrowseripod() { if( stripos($this->_agent,'ipod') !== false ) { $aresult = explode('/',stristr($this->_agent,'version')); if( isset($aresult[1]) ) { $aversion = explode(' ',$aresult[1]); $this->setversion($aversion[0]); } else $this->setversion(self::version_unknown); $this->setmobile(true); $this->setbrowser(self::browser_ipod); return true; } return false; } /** * determine if the browser is android or not (last updated 1.7) * @return boolean true if the browser is android otherwise false */ protected function checkbrowserandroid() { if( stripos($this->_agent,'android') !== false ) { $aresult = explode(' ',stristr($this->_agent,'android')); if( isset($aresult[1]) ) { $aversion = explode(' ',$aresult[1]); $this->setversion($aversion[0]); } else $this->setversion(self::version_unknown); $this->setmobile(true); $this->setbrowser(self::browser_android); return true; } return false; } /** * determine the user's platform (last updated 1.7) */ protected function checkplatform() { if( stripos($this->_agent, 'windows') !== false ) $this->_platform = self::platform_windows; else if( stripos($this->_agent, 'ipad') !== false ) $this->_platform = self::platform_ipad; else if( stripos($this->_agent, 'ipod') !== false ) $this->_platform = self::platform_ipod; else if( stripos($this->_agent, 'iphone') !== false ) $this->_platform = self::platform_iphone; elseif( stripos($this->_agent, 'mac') !== false ) $this->_platform = self::platform_apple; elseif( stripos($this->_agent, 'android') !== false ) $this->_platform = self::platform_android; elseif( stripos($this->_agent, 'linux') !== false ) $this->_platform = self::platform_linux; else if( stripos($this->_agent, 'nokia') !== false ) $this->_platform = self::platform_nokia; else if( stripos($this->_agent, 'blackberry') !== false ) $this->_platform = self::platform_blackberry; elseif( stripos($this->_agent,'freebsd') !== false ) $this->_platform = self::platform_freebsd; elseif( stripos($this->_agent,'openbsd') !== false ) $this->_platform = self::platform_openbsd; elseif( stripos($this->_agent,'netbsd') !== false ) $this->_platform = self::platform_netbsd; elseif( stripos($this->_agent, 'opensolaris') !== false ) $this->_platform = self::platform_opensolaris; elseif( stripos($this->_agent, 'sunos') !== false ) $this->_platform = self::platform_sunos; elseif( stripos($this->_agent, 'os\/2') !== false ) $this->_platform = self::platform_os2; elseif( stripos($this->_agent, 'beos') !== false ) $this->_platform = self::platform_beos; elseif( stripos($this->_agent, 'win') !== false ) $this->_platform = self::platform_windows; }}
这个类的用法也很简单,如下:
$obj_browser = new browser; //创建一个此类的一个实例$obj_browser->getbrowser(); //调用相关函数,获取浏览器名称$obj_browser->getplatform(); //调用相关函数,获取系统名称$obj_browser->ismobile(); //判断来访者所用设备是iphone、ipad或者电脑(pc)
好了,到此为止php获取服务器ie版本,通过php判断浏览器类型的方法就总结了。有三种方法,但是如果准确率不是那么高,第一种完全可以,第二种和第三种方法有点过于精细, 无法确定复杂性但很明显。
另外,如果想知道如何判断浏览器类型,可以参考本站文章:
如何获取浏览器类型和版本号
建个网站需要多少钱?“朋友圈点赞”的分析与思考网站关键词应该如何优化 这些事项要掌握企业网站怎样添加友情链接?互联网时代,企业要如何建立网站才能获得更多的用户和流量呢?学校网站建设制作设计解决方案2019年最新seo优化方案建设企业网站如何才能找一家靠谱的网建公司(怎么样建设企业网站)