Browser Detection


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

Heres how to use
print_r(browser_detection('full'));

We can also use this script
print "IP: ". $_SERVER['REMOTE_ADDR'].""; ### IP ADDRESS ##
print "Browser:" . $_SERVER["HTTP_USER_AGENT"].""; ## Browser
print "OS: ". $_ENV["OS"]; ### OS


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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.