Browser Detection


/ Published in: PHP
Save to your folder(s)

echo ( browser_detection( 'number' ) .''.
browser_detection( 'browser' ) .''.
browser_detection( 'os' ) .''.
browser_detection( 'os_number' ) );

Outputs (browser version, browser, os, os number):
1.5
moz
nt
5.1


if ( ( browser_detection( 'browser' ) == 'ie' )
&&
( browser_detection( 'number' ) >= 5 ) )
{
echo 'it is Internet Explorer ' .
browser_detection( 'number' );
// or anything else you want to happen of course
}


Copy this code and paste it in your HTML
  1. <?php
  2. /*
  3. Script Name: Full Featured PHP Browser/OS detection
  4. Author: Harald Hope, Website: http://techpatterns.com/
  5. Script Source URI: http://techpatterns.com/downloads/php_browser_detection.php
  6. Version 4.9.11
  7. Copyright (C) 29 June 2007
  8.  
  9. This program is free software; you can redistribute it and/or modify it under
  10. the terms of the GNU General Public License as published by the Free Software
  11. Foundation; either version 3 of the License, or (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful, but WITHOUT
  14. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  16.  
  17. Get the full text of the GPL here: http://www.gnu.org/licenses/gpl.txt
  18.  
  19. Coding conventions:
  20. http://cvs.sourceforge.net/viewcvs.py/phpbb/phpBB2/docs/codingstandards.htm?rev=1.3
  21. */
  22.  
  23. /******************************************
  24. this is currently set to accept 11 parameters, although you can add as many as you want:
  25. 1. safe - returns true/false, you can determine what makes the browser be safe lower down,
  26. currently it's set for ns4 and pre version 1 mozillas not being safe, plus all older browsers
  27. 2. ie_version - tests to see what general IE it is, ie5x-6, ie4, or ieMac, returns these values.
  28. 3. moz_version - returns array of moz version, version number (includes full version, + etc), rv number (for math
  29. comparison), rv number (for full rv, including alpha and beta versions), and release date
  30. 4. dom - returns true/false if it is a basic dom browser, ie >= 5, opera >= 5, all new mozillas, safaris, konquerors
  31. 5. os - returns which os is being used
  32. 6. os_number - returns windows versions, 95, 98, me, nt 4, nt 5 [windows 2000], nt 5.1 [windows xp],
  33. Just added: os x detection[crude] otherwise returns false
  34. 7. browser - returns the browser name, in shorthand: ie, ie4, ie5x, op, moz, konq, saf, ns4
  35. 8. number - returns the browser version number, if available, otherwise returns '' [not available]
  36. 9. full - returns this array: $browser_name, $version_number, $ie_version, $dom_browser,
  37. $safe_browser, $os, $os_number, $s_browser [the browser search string from the browser array], $type
  38. 10. type - returns whether it's a bot or a browser
  39. 11. math_number - returns basic version number, for math comparison, ie. 1.2rel2a becomes 1.2
  40. *******************************************/
  41.  
  42. // main script, uses two other functions, which_os() and browser_version() as needed
  43. function browser_detection( $which_test ) {
  44. /*
  45. uncomment the global variable declaration if you want the variables to be available on a global level
  46. throughout your php page, make sure that php is configured to support the use of globals first!
  47. Use of globals should be avoided however, and they are not necessary with this script
  48. */
  49.  
  50. /*global $dom_browser, $safe_browser, $browser_user_agent, $os, $browser_name, $s_browser, $ie_version,
  51. $version_number, $os_number, $b_repeat, $moz_version, $moz_version_number, $moz_rv, $moz_rv_full, $moz_release;*/
  52.  
  53. static $dom_browser, $safe_browser, $browser_user_agent, $os, $browser_name, $s_browser, $ie_version,
  54. $version_number, $os_number, $b_repeat, $moz_version, $moz_version_number, $moz_rv, $moz_rv_full, $moz_release,
  55. $type, $math_version_number;
  56.  
  57. /*
  58. this makes the test only run once no matter how many times you call it
  59. since all the variables are filled on the first run through, it's only a matter of returning the
  60. the right ones
  61. */
  62. if ( !$b_repeat )
  63. {
  64. //initialize all variables with default values to prevent error
  65. $dom_browser = false;
  66. $type = 'bot';// default to bot since you never know with bots
  67. $safe_browser = false;
  68. $os = '';
  69. $os_number = '';
  70. $a_os_data = '';
  71. $browser_name = '';
  72. $version_number = '';
  73. $math_version_number = '';
  74. $ie_version = '';
  75. $moz_version = '';
  76. $moz_version_number = '';
  77. $moz_rv = '';
  78. $moz_rv_full = '';
  79. $moz_release = '';
  80. $b_success = false;// boolean for if browser found in main test
  81.  
  82. //make navigator user agent string lower case to make sure all versions get caught
  83. // isset protects against blank user agent failure
  84. $browser_user_agent = ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) ? strtolower( $_SERVER['HTTP_USER_AGENT'] ) : '';
  85.  
  86. /*
  87. pack the browser type array, in this order
  88. the order is important, because opera must be tested first, then omniweb [which has safari data in string],
  89. same for konqueror, then safari, then gecko, since safari navigator user agent id's with 'gecko' in string.
  90. note that $dom_browser is set for all modern dom browsers, this gives you a default to use.
  91.  
  92. array[0] = id string for useragent, array[1] is if dom capable, array[2] is working name for browser,
  93. array[3] identifies navigator useragent type
  94.  
  95. Note: all browser strings are in lower case to match the strtolower output, this avoids possible detection
  96. errors
  97.  
  98. Note: There are currently 5 navigator user agent types:
  99. bro - modern, css supporting browser.
  100. bbro - basic browser, text only, table only, defective css implementation
  101. bot - search type spider
  102. dow - known download agent
  103. lib - standard http libraries
  104. */
  105. // known browsers, list will be updated routinely, check back now and then
  106. $a_browser_types[] = array( 'opera', true, 'op', 'bro' );
  107. $a_browser_types[] = array( 'omniweb', true, 'omni', 'bro' );// mac osx browser, now uses khtml engine:
  108. $a_browser_types[] = array( 'msie', true, 'ie', 'bro' );
  109. $a_browser_types[] = array( 'konqueror', true, 'konq', 'bro' );
  110. $a_browser_types[] = array( 'safari', true, 'saf', 'bro' );
  111. // covers Netscape 6-7, K-Meleon, Most linux versions, uses moz array below
  112. $a_browser_types[] = array( 'gecko', true, 'moz', 'bro' );
  113. $a_browser_types[] = array( 'netpositive', false, 'netp', 'bbro' );// beos browser
  114. $a_browser_types[] = array( 'lynx', false, 'lynx', 'bbro' ); // command line browser
  115. $a_browser_types[] = array( 'elinks ', false, 'elinks', 'bbro' ); // new version of links
  116. $a_browser_types[] = array( 'elinks', false, 'elinks', 'bbro' ); // alternate id for it
  117. $a_browser_types[] = array( 'links ', false, 'links', 'bbro' ); // old name for links
  118. $a_browser_types[] = array( 'links', false, 'links', 'bbro' ); // alternate id for it
  119. $a_browser_types[] = array( 'w3m', false, 'w3m', 'bbro' ); // open source browser, more features than lynx/links
  120. $a_browser_types[] = array( 'webtv', false, 'webtv', 'bbro' );// junk ms webtv
  121. $a_browser_types[] = array( 'amaya', false, 'amaya', 'bbro' );// w3c browser
  122. $a_browser_types[] = array( 'dillo', false, 'dillo', 'bbro' );// linux browser, basic table support
  123. $a_browser_types[] = array( 'ibrowse', false, 'ibrowse', 'bbro' );// amiga browser
  124. $a_browser_types[] = array( 'icab', false, 'icab', 'bro' );// mac browser
  125. $a_browser_types[] = array( 'crazy browser', true, 'ie', 'bro' );// uses ie rendering engine
  126. $a_browser_types[] = array( 'sonyericssonp800', false, 'sonyericssonp800', 'bbro' );// sony ericsson handheld
  127.  
  128. // search engine spider bots:
  129. $a_browser_types[] = array( 'googlebot', false, 'google', 'bot' );// google
  130. $a_browser_types[] = array( 'mediapartners-google', false, 'adsense', 'bot' );// google adsense
  131. $a_browser_types[] = array( 'yahoo-verticalcrawler', false, 'yahoo', 'bot' );// old yahoo bot
  132. $a_browser_types[] = array( 'yahoo! slurp', false, 'yahoo', 'bot' ); // new yahoo bot
  133. $a_browser_types[] = array( 'yahoo-mm', false, 'yahoomm', 'bot' ); // gets Yahoo-MMCrawler and Yahoo-MMAudVid bots
  134. $a_browser_types[] = array( 'inktomi', false, 'inktomi', 'bot' ); // inktomi bot
  135. $a_browser_types[] = array( 'slurp', false, 'inktomi', 'bot' ); // inktomi bot
  136. $a_browser_types[] = array( 'fast-webcrawler', false, 'fast', 'bot' );// Fast AllTheWeb
  137. $a_browser_types[] = array( 'msnbot', false, 'msn', 'bot' );// msn search
  138. $a_browser_types[] = array( 'ask jeeves', false, 'ask', 'bot' ); //jeeves/teoma
  139. $a_browser_types[] = array( 'teoma', false, 'ask', 'bot' );//jeeves teoma
  140. $a_browser_types[] = array( 'scooter', false, 'scooter', 'bot' );// altavista
  141. $a_browser_types[] = array( 'openbot', false, 'openbot', 'bot' );// openbot, from taiwan
  142. $a_browser_types[] = array( 'ia_archiver', false, 'ia_archiver', 'bot' );// ia archiver
  143. $a_browser_types[] = array( 'zyborg', false, 'looksmart', 'bot' );// looksmart
  144. $a_browser_types[] = array( 'almaden', false, 'ibm', 'bot' );// ibm almaden web crawler
  145. $a_browser_types[] = array( 'baiduspider', false, 'baidu', 'bot' );// Baiduspider asian search spider
  146. $a_browser_types[] = array( 'psbot', false, 'psbot', 'bot' );// psbot image crawler
  147. $a_browser_types[] = array( 'gigabot', false, 'gigabot', 'bot' );// gigabot crawler
  148. $a_browser_types[] = array( 'naverbot', false, 'naverbot', 'bot' );// naverbot crawler, bad bot, block
  149. $a_browser_types[] = array( 'surveybot', false, 'surveybot', 'bot' );//
  150. $a_browser_types[] = array( 'boitho.com-dc', false, 'boitho', 'bot' );//norwegian search engine
  151. $a_browser_types[] = array( 'objectssearch', false, 'objectsearch', 'bot' );// open source search engine
  152. $a_browser_types[] = array( 'answerbus', false, 'answerbus', 'bot' );// http://www.answerbus.com/, web questions
  153. $a_browser_types[] = array( 'sohu-search', false, 'sohu', 'bot' );// chinese media company, search component
  154. $a_browser_types[] = array( 'iltrovatore-setaccio', false, 'il-set', 'bot' );
  155.  
  156. // various http utility libaries
  157. $a_browser_types[] = array( 'w3c_validator', false, 'w3c', 'lib' ); // uses libperl, make first
  158. $a_browser_types[] = array( 'wdg_validator', false, 'wdg', 'lib' ); //
  159. $a_browser_types[] = array( 'libwww-perl', false, 'libwww-perl', 'lib' );
  160. $a_browser_types[] = array( 'jakarta commons-httpclient', false, 'jakarta', 'lib' );
  161. $a_browser_types[] = array( 'python-urllib', false, 'python-urllib', 'lib' );
  162.  
  163. // download apps
  164. $a_browser_types[] = array( 'getright', false, 'getright', 'dow' );
  165. $a_browser_types[] = array( 'wget', false, 'wget', 'dow' );// open source downloader, obeys robots.txt
  166.  
  167. // netscape 4 and earlier tests, put last so spiders don't get caught
  168. $a_browser_types[] = array( 'mozilla/4.', false, 'ns', 'bbro' );
  169. $a_browser_types[] = array( 'mozilla/3.', false, 'ns', 'bbro' );
  170. $a_browser_types[] = array( 'mozilla/2.', false, 'ns', 'bbro' );
  171.  
  172. //$a_browser_types[] = array( '', false ); // browser array template
  173.  
  174. /*
  175. moz types array
  176. note the order, netscape6 must come before netscape, which is how netscape 7 id's itself.
  177. rv comes last in case it is plain old mozilla
  178. */
  179. $moz_types = array( 'firebird', 'phoenix', 'firefox', 'iceweasel', 'galeon', 'k-meleon', 'camino', 'epiphany', 'netscape6', 'netscape', 'multizilla', 'rv' );
  180.  
  181. /*
  182. run through the browser_types array, break if you hit a match, if no match, assume old browser
  183. or non dom browser, assigns false value to $b_success.
  184. */
  185. for ($i = 0; $i < count($a_browser_types); $i++)
  186. {
  187. //unpacks browser array, assigns to variables
  188. $s_browser = $a_browser_types[$i][0];// text string to id browser from array
  189.  
  190. if (stristr($browser_user_agent, $s_browser))
  191. {
  192. // it defaults to true, will become false below if needed
  193. // this keeps it easier to keep track of what is safe, only
  194. //explicit false assignment will make it false.
  195. $safe_browser = true;
  196.  
  197. // assign values based on match of user agent string
  198. $dom_browser = $a_browser_types[$i][1];// hardcoded dom support from array
  199. $browser_name = $a_browser_types[$i][2];// working name for browser
  200. $type = $a_browser_types[$i][3];// sets whether bot or browser
  201.  
  202. switch ( $browser_name )
  203. {
  204. // this is modified quite a bit, now will return proper netscape version number
  205. // check your implementation to make sure it works
  206. case 'ns':
  207. $safe_browser = false;
  208. $version_number = browser_version( $browser_user_agent, 'mozilla' );
  209. break;
  210. case 'moz':
  211. /*
  212. note: The 'rv' test is not absolute since the rv number is very different on
  213. different versions, for example Galean doesn't use the same rv version as Mozilla,
  214. neither do later Netscapes, like 7.x. For more on this, read the full mozilla numbering
  215. conventions here:
  216. http://www.mozilla.org/releases/cvstags.html
  217. */
  218.  
  219. // this will return alpha and beta version numbers, if present
  220. $moz_rv_full = browser_version( $browser_user_agent, 'rv' );
  221. // this slices them back off for math comparisons
  222. $moz_rv = substr( $moz_rv_full, 0, 3 );
  223.  
  224. // this is to pull out specific mozilla versions, firebird, netscape etc..
  225. for ( $i = 0; $i < count( $moz_types ); $i++ )
  226. {
  227. if ( stristr( $browser_user_agent, $moz_types[$i] ) )
  228. {
  229. $moz_version = $moz_types[$i];
  230. $moz_version_number = browser_version( $browser_user_agent, $moz_version );
  231. break;
  232. }
  233. }
  234. // this is necesary to protect against false id'ed moz'es and new moz'es.
  235. // this corrects for galeon, or any other moz browser without an rv number
  236. if ( !$moz_rv )
  237. {
  238. $moz_rv = substr( $moz_version_number, 0, 3 );
  239. $moz_rv_full = $moz_version_number;
  240. /*
  241. // you can use this instead if you are running php >= 4.2
  242. $moz_rv = floatval( $moz_version_number );
  243. $moz_rv_full = $moz_version_number;
  244. */
  245. }
  246. // this corrects the version name in case it went to the default 'rv' for the test
  247. if ( $moz_version == 'rv' )
  248. {
  249. $moz_version = 'mozilla';
  250. }
  251.  
  252. //the moz version will be taken from the rv number, see notes above for rv problems
  253. $version_number = $moz_rv;
  254. // gets the actual release date, necessary if you need to do functionality tests
  255. $moz_release = browser_version( $browser_user_agent, 'gecko/' );
  256. /*
  257. Test for mozilla 0.9.x / netscape 6.x
  258. test your javascript/CSS to see if it works in these mozilla releases, if it does, just default it to:
  259. $safe_browser = true;
  260. */
  261. if ( ( $moz_release < 20020400 ) || ( $moz_rv < 1 ) )
  262. {
  263. $safe_browser = false;
  264. }
  265. break;
  266. case 'ie':
  267. $version_number = browser_version( $browser_user_agent, $s_browser );
  268. // first test for IE 5x mac, that's the most problematic IE out there
  269. if ( stristr( $browser_user_agent, 'mac') )
  270. {
  271. $ie_version = 'ieMac';
  272. }
  273. // this assigns a general ie id to the $ie_version variable
  274. elseif ( $version_number >= 5 )
  275. {
  276. $ie_version = 'ie5x';
  277. }
  278. elseif ( ( $version_number > 3 ) && ( $version_number < 5 ) )
  279. {
  280. $dom_browser = false;
  281. $ie_version = 'ie4';
  282. // this depends on what you're using the script for, make sure this fits your needs
  283. $safe_browser = true;
  284. }
  285. else
  286. {
  287. $ie_version = 'old';
  288. $dom_browser = false;
  289. $safe_browser = false;
  290. }
  291. break;
  292. case 'op':
  293. $version_number = browser_version( $browser_user_agent, $s_browser );
  294. if ( $version_number < 5 )// opera 4 wasn't very useable.
  295. {
  296. $safe_browser = false;
  297. }
  298. break;
  299. case 'saf':
  300. $version_number = browser_version( $browser_user_agent, $s_browser );
  301. break;
  302. /*
  303. Uncomment this section if you want omniweb to return the safari value
  304. Omniweb uses khtml/safari rendering engine, so you can treat it like
  305. safari if you want.
  306. */
  307. /*
  308. case 'omni':
  309. $s_browser = 'safari';
  310. $browser_name = 'saf';
  311. $version_number = browser_version( $browser_user_agent, 'applewebkit' );
  312. break;
  313. */
  314. default:
  315. $version_number = browser_version( $browser_user_agent, $s_browser );
  316. break;
  317. }
  318. // the browser was id'ed
  319. $b_success = true;
  320. break;
  321. }
  322. }
  323.  
  324. //assigns defaults if the browser was not found in the loop test
  325. if ( !$b_success )
  326. {
  327. /*
  328. this will return the first part of the browser string if the above id's failed
  329. usually the first part of the browser string has the navigator useragent name/version in it.
  330. This will usually correctly id the browser and the browser number if it didn't get
  331. caught by the above routine.
  332. If you want a '' to do a if browser == '' type test, just comment out all lines below
  333. except for the last line, and uncomment the last line. If you want undefined values,
  334. the browser_name is '', you can always test for that
  335. */
  336. // delete this part if you want an unknown browser returned
  337. $s_browser = substr( $browser_user_agent, 0, strcspn( $browser_user_agent , '();') );
  338. // this extracts just the browser name from the string
  339. ereg('[^0-9][a-z]*-*\ *[a-z]*\ *[a-z]*', $s_browser, $r );
  340. $s_browser = $r[0];
  341. $version_number = browser_version( $browser_user_agent, $s_browser );
  342.  
  343. // then uncomment this part
  344. //$s_browser = '';//deletes the last array item in case the browser was not a match
  345. }
  346. // get os data, mac os x test requires browser/version information, this is a change from older scripts
  347. $a_os_data = which_os( $browser_user_agent, $browser_name, $version_number );
  348. $os = $a_os_data[0];// os name, abbreviated
  349. $os_number = $a_os_data[1];// os number or version if available
  350.  
  351. // this ends the run through once if clause, set the boolean
  352. //to true so the function won't retest everything
  353. $b_repeat = true;
  354.  
  355. // pulls out primary version number from more complex string, like 7.5a,
  356. // use this for numeric version comparison
  357. $m = array();
  358. if ( ereg('[0-9]*\.*[0-9]*', $version_number, $m ) )
  359. {
  360. $math_version_number = $m[0];
  361. //print_r($m);
  362. }
  363.  
  364. }
  365. //$version_number = $_SERVER["REMOTE_ADDR"];
  366. /*
  367. This is where you return values based on what parameter you used to call the function
  368. $which_test is the passed parameter in the initial browser_detection('os') for example call
  369. */
  370. switch ( $which_test )
  371. {
  372. case 'safe':// returns true/false if your tests determine it's a safe browser
  373. // you can change the tests to determine what is a safeBrowser for your scripts
  374. // in this case sub rv 1 Mozillas and Netscape 4x's trigger the unsafe condition
  375. return $safe_browser;
  376. break;
  377. case 'ie_version': // returns ieMac or ie5x
  378. return $ie_version;
  379. break;
  380. case 'moz_version':// returns array of all relevant moz information
  381. $moz_array = array( $moz_version, $moz_version_number, $moz_rv, $moz_rv_full, $moz_release );
  382. return $moz_array;
  383. break;
  384. case 'dom':// returns true/fale if a DOM capable browser
  385. return $dom_browser;
  386. break;
  387. case 'os':// returns os name
  388. return $os;
  389. break;
  390. case 'os_number':// returns os number if windows
  391. return $os_number;
  392. break;
  393. case 'browser':// returns browser name
  394. return $browser_name;
  395. break;
  396. case 'number':// returns browser number
  397. return $version_number;
  398. break;
  399. case 'full':// returns all relevant browser information in an array
  400. $full_array = array( $browser_name, $version_number, $ie_version, $dom_browser, $safe_browser,
  401. $os, $os_number, $s_browser, $type, $math_version_number );
  402. return $full_array;
  403. break;
  404. case 'type':// returns what type, bot, browser, maybe downloader in future
  405. return $type;
  406. break;
  407. case 'math_number':// returns numerical version number, for number comparisons
  408. return $math_version_number;
  409. break;
  410. default:
  411. break;
  412. }
  413. }
  414.  
  415. // gets which os from the browser string
  416. function which_os ( $browser_string, $browser_name, $version_number )
  417. {
  418. // initialize variables
  419. $os = '';
  420. $os_version = '';
  421. /*
  422. packs the os array
  423. use this order since some navigator user agents will put 'macintosh' in the navigator user agent string
  424. which would make the nt test register true
  425. */
  426. $a_mac = array( 'mac68k', 'macppc' );// this is not used currently
  427. // same logic, check in order to catch the os's in order, last is always default item
  428. $a_unix = array( 'unixware', 'solaris', 'sunos', 'sun4', 'sun5', 'suni86', 'sun',
  429. 'freebsd', 'openbsd', 'bsd' , 'irix5', 'irix6', 'irix', 'hpux9', 'hpux10', 'hpux11', 'hpux', 'hp-ux',
  430. 'aix1', 'aix2', 'aix3', 'aix4', 'aix5', 'aix', 'sco', 'unixware', 'mpras', 'reliant',
  431. 'dec', 'sinix', 'unix' );
  432. // only sometimes will you get a linux distro to id itself...
  433. $a_linux = array( 'kanotix', 'ubuntu', 'mepis', 'debian', 'suse', 'redhat', 'slackware', 'mandrake', 'gentoo', 'linux' );
  434. $a_linux_process = array ( 'i386', 'i586', 'i686' );// not use currently
  435. // note, order of os very important in os array, you will get failed ids if changed
  436. $a_os = array( 'beos', 'os2', 'amiga', 'webtv', 'mac', 'nt', 'win', $a_unix, $a_linux );
  437.  
  438. //os tester
  439. for ( $i = 0; $i < count( $a_os ); $i++ )
  440. {
  441. //unpacks os array, assigns to variable
  442. $s_os = $a_os[$i];
  443.  
  444. //assign os to global os variable, os flag true on success
  445. //!stristr($browser_string, "linux" ) corrects a linux detection bug
  446. if ( !is_array( $s_os ) && stristr( $browser_string, $s_os ) && !stristr( $browser_string, "linux" ) )
  447. {
  448. $os = $s_os;
  449.  
  450. switch ( $os )
  451. {
  452. case 'win':
  453. if ( strstr( $browser_string, '95' ) )
  454. {
  455. $os_version = '95';
  456. }
  457. elseif ( ( strstr( $browser_string, '9x 4.9' ) ) || ( strstr( $browser_string, 'me' ) ) )
  458. {
  459. $os_version = 'me';
  460. }
  461. elseif ( strstr( $browser_string, '98' ) )
  462. {
  463. $os_version = '98';
  464. }
  465. elseif ( strstr( $browser_string, '2000' ) )// windows 2000, for opera ID
  466. {
  467. $os_version = 5.0;
  468. $os = 'nt';
  469. }
  470. elseif ( strstr( $browser_string, 'xp' ) )// windows 2000, for opera ID
  471. {
  472. $os_version = 5.1;
  473. $os = 'nt';
  474. }
  475. elseif ( strstr( $browser_string, '2003' ) )// windows server 2003, for opera ID
  476. {
  477. $os_version = 5.2;
  478. $os = 'nt';
  479. }
  480. elseif ( strstr( $browser_string, 'ce' ) )// windows CE
  481. {
  482. $os_version = 'ce';
  483. }
  484. break;
  485. case 'nt':
  486. if ( strstr( $browser_string, 'nt 5.2' ) )// windows server 2003
  487. {
  488. $os_version = 5.2;
  489. $os = 'nt';
  490. }
  491. elseif ( strstr( $browser_string, 'nt 5.1' ) || strstr( $browser_string, 'xp' ) )// windows xp
  492. {
  493. $os_version = 5.1;//
  494. }
  495. elseif ( strstr( $browser_string, 'nt 5' ) || strstr( $browser_string, '2000' ) )// windows 2000
  496. {
  497. $os_version = 5.0;
  498. }
  499. elseif ( strstr( $browser_string, 'nt 4' ) )// nt 4
  500. {
  501. $os_version = 4;
  502. }
  503. elseif ( strstr( $browser_string, 'nt 3' ) )// nt 4
  504. {
  505. $os_version = 3;
  506. }
  507. break;
  508. case 'mac':
  509. if ( strstr( $browser_string, 'os x' ) )
  510. {
  511. $os_version = 10;
  512. }
  513. //this is a crude test for os x, since safari, camino, ie 5.2, & moz >= rv 1.3
  514. //are only made for os x
  515. elseif ( ( $browser_name == 'saf' ) || ( $browser_name == 'cam' ) ||
  516. ( ( $browser_name == 'moz' ) && ( $version_number >= 1.3 ) ) ||
  517. ( ( $browser_name == 'ie' ) && ( $version_number >= 5.2 ) ) )
  518. {
  519. $os_version = 10;
  520. }
  521. break;
  522. default:
  523. break;
  524. }
  525. break;
  526. }
  527. // check that it's an array, check it's the second to last item
  528. //in the main os array, the unix one that is
  529. elseif ( is_array( $s_os ) && ( $i == ( count( $a_os ) - 2 ) ) )
  530. {
  531. for ($j = 0; $j < count($s_os); $j++)
  532. {
  533. if ( stristr( $browser_string, $s_os[$j] ) )
  534. {
  535. $os = 'unix'; //if the os is in the unix array, it's unix, obviously...
  536. $os_version = ( $s_os[$j] != 'unix' ) ? $s_os[$j] : '';// assign sub unix version from the unix array
  537. break;
  538. }
  539. }
  540. }
  541. // check that it's an array, check it's the last item
  542. //in the main os array, the linux one that is
  543. elseif ( is_array( $s_os ) && ( $i == ( count( $a_os ) - 1 ) ) )
  544. {
  545. for ($j = 0; $j < count($s_os); $j++)
  546. {
  547. if ( stristr( $browser_string, $s_os[$j] ) )
  548. {
  549. $os = 'lin';
  550. // assign linux distro from the linux array, there's a default
  551. //search for 'lin', if it's that, set version to ''
  552. $os_version = ( $s_os[$j] != 'linux' ) ? $s_os[$j] : '';
  553. break;
  554. }
  555. }
  556. }
  557. }
  558.  
  559. // pack the os data array for return to main function
  560. $os_data = array( $os, $os_version );
  561. return $os_data;
  562. }
  563.  
  564. // function returns browser number, gecko rv number, or gecko release date
  565. //function browser_version( $browser_user_agent, $search_string, $substring_length )
  566. function browser_version( $browser_user_agent, $search_string )
  567. {
  568. // 12 is the longest that will be required, handles release dates: 20020323; 0.8.0+
  569. $substring_length = 12;
  570. //initialize browser number, will return '' if not found
  571. $browser_number = '';
  572.  
  573. // use the passed parameter for $search_string
  574. // start the substring slice right after these moz search strings
  575. // there are some cases of double msie id's, first in string and then with then number
  576. $start_pos = 0;
  577. /* this test covers you for multiple occurrences of string, only with ie though
  578. with for example google bot you want the first occurance returned, since that's where the
  579. numbering happens */
  580.  
  581. for ( $i = 0; $i < 4; $i++ )
  582. {
  583. //start the search after the first string occurrence
  584. if ( strpos( $browser_user_agent, $search_string, $start_pos ) !== false )
  585. {
  586. //update start position if position found
  587. $start_pos = strpos( $browser_user_agent, $search_string, $start_pos ) + strlen( $search_string );
  588. if ( $search_string != 'msie' )
  589. {
  590. break;
  591. }
  592. }
  593. else
  594. {
  595. break;
  596. }
  597. }
  598.  
  599. // this is just to get the release date, not other moz information
  600. // also corrects for the omniweb 'v'
  601. if ( $search_string != 'gecko/' )
  602. {
  603. if ( $search_string == 'omniweb' )
  604. {
  605. $start_pos += 2;// handles the v in 'omniweb/v532.xx
  606. }
  607. else
  608. {
  609. $start_pos++;
  610. }
  611. }
  612.  
  613. // Initial trimming
  614. $browser_number = substr( $browser_user_agent, $start_pos, $substring_length );
  615.  
  616. // Find the space, ;, or parentheses that ends the number
  617. $browser_number = substr( $browser_number, 0, strcspn($browser_number, ' );') );
  618.  
  619. //make sure the returned value is actually the id number and not a string
  620. // otherwise return ''
  621. if ( !is_numeric( substr( $browser_number, 0, 1 ) ) )
  622. {
  623. $browser_number = '';
  624. }
  625. //$browser_number = strrpos( $browser_user_agent, $search_string );
  626. return $browser_number;
  627. }
  628.  
  629. /*
  630. Here are some typical navigator.userAgent strings so you can see where the data comes from
  631. Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.5) Gecko/20031007 Firebird/0.7
  632. Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:0.9.4) Gecko/20011128 Netscape6/6.2.1
  633. */
  634. ?>

URL: http://techpatterns.com/downloads/php_browser_detection.php

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.