Posted By

localhorst on 03/17/08


Tagged

browser detection browserdetection


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

vali29
Juntalis


Browser class


 / Published in: PHP
 

Identifies the user's Operating system, browser and version by parsing the HTTPUSERAGENT string sent to the server For browsers, it should correctly identify all versions of: Amaya Galeon iCab Internet Explorer For AOL versions it will identify as Internet Explorer (AOL) and the version will be the AOL version instead of the IE version. Konqueror Lynx Mozilla Netscape Navigator/Communicator OmniWeb Opera Pocket Internet Explorer for handhelds Safari WebTV

  1. /*****************************************************************
  2.  
  3. File name: browser.php
  4. Author: Gary White
  5. Last modified: November 10, 2003
  6.  
  7. **************************************************************
  8.  
  9. Copyright (C) 2003 Gary White
  10.  
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details at:
  20. http://www.gnu.org/copyleft/gpl.html
  21.  
  22. **************************************************************
  23.  
  24. Browser class
  25.  
  26. Identifies the user's Operating system, browser and version
  27. by parsing the HTTP_USER_AGENT string sent to the server
  28.  
  29. Typical Usage:
  30.  
  31. require_once($_SERVER['DOCUMENT_ROOT'].'/include/browser.php');
  32. $br = new Browser;
  33. echo "$br->Platform, $br->Name version $br->Version";
  34.  
  35. For operating systems, it will correctly identify:
  36. Microsoft Windows
  37. MacIntosh
  38. Linux
  39.  
  40. Anything not determined to be one of the above is considered to by Unix
  41. because most Unix based browsers seem to not report the operating system.
  42. The only known problem here is that, if a HTTP_USER_AGENT string does not
  43. contain the operating system, it will be identified as Unix. For unknown
  44. browsers, this may not be correct.
  45.  
  46. For browsers, it should correctly identify all versions of:
  47. Amaya
  48. Galeon
  49. iCab
  50. Internet Explorer
  51. For AOL versions it will identify as Internet Explorer (AOL) and the version
  52. will be the AOL version instead of the IE version.
  53. Konqueror
  54. Lynx
  55. Mozilla
  56. Netscape Navigator/Communicator
  57. OmniWeb
  58. Opera
  59. Pocket Internet Explorer for handhelds
  60. Safari
  61. WebTV
  62. *****************************************************************/
  63.  
  64. class browser{
  65.  
  66. var $Name = "Unknown";
  67. var $Version = "Unknown";
  68. var $Platform = "Unknown";
  69. var $UserAgent = "Not reported";
  70. var $AOL = false;
  71.  
  72. function browser(){
  73. $agent = $_SERVER['HTTP_USER_AGENT'];
  74.  
  75. // initialize properties
  76. $bd['platform'] = "Unknown";
  77. $bd['browser'] = "Unknown";
  78. $bd['version'] = "Unknown";
  79. $this->UserAgent = $agent;
  80.  
  81. // find operating system
  82. if (eregi("win", $agent))
  83. $bd['platform'] = "Windows";
  84. elseif (eregi("mac", $agent))
  85. $bd['platform'] = "MacIntosh";
  86. elseif (eregi("linux", $agent))
  87. $bd['platform'] = "Linux";
  88. elseif (eregi("OS/2", $agent))
  89. $bd['platform'] = "OS/2";
  90. elseif (eregi("BeOS", $agent))
  91. $bd['platform'] = "BeOS";
  92.  
  93. // test for Opera
  94. if (eregi("opera",$agent)){
  95. $val = stristr($agent, "opera");
  96. if (eregi("/", $val)){
  97. $val = explode("/",$val);
  98. $bd['browser'] = $val[0];
  99. $val = explode(" ",$val[1]);
  100. $bd['version'] = $val[0];
  101. }else{
  102. $val = explode(" ",stristr($val,"opera"));
  103. $bd['browser'] = $val[0];
  104. $bd['version'] = $val[1];
  105. }
  106.  
  107. // test for WebTV
  108. }elseif(eregi("webtv",$agent)){
  109. $val = explode("/",stristr($agent,"webtv"));
  110. $bd['browser'] = $val[0];
  111. $bd['version'] = $val[1];
  112.  
  113. // test for MS Internet Explorer version 1
  114. }elseif(eregi("microsoft internet explorer", $agent)){
  115. $bd['browser'] = "MSIE";
  116. $bd['version'] = "1.0";
  117. $var = stristr($agent, "/");
  118. if (ereg("308|425|426|474|0b1", $var)){
  119. $bd['version'] = "1.5";
  120. }
  121.  
  122. // test for NetPositive
  123. }elseif(eregi("NetPositive", $agent)){
  124. $val = explode("/",stristr($agent,"NetPositive"));
  125. $bd['platform'] = "BeOS";
  126. $bd['browser'] = $val[0];
  127. $bd['version'] = $val[1];
  128.  
  129. // test for MS Internet Explorer
  130. }elseif(eregi("msie",$agent) && !eregi("opera",$agent)){
  131. $val = explode(" ",stristr($agent,"msie"));
  132. $bd['browser'] = $val[0];
  133. $bd['version'] = $val[1];
  134.  
  135. // test for MS Pocket Internet Explorer
  136. }elseif(eregi("mspie",$agent) || eregi('pocket', $agent)){
  137. $val = explode(" ",stristr($agent,"mspie"));
  138. $bd['browser'] = "MSPIE";
  139. $bd['platform'] = "WindowsCE";
  140. if (eregi("mspie", $agent))
  141. $bd['version'] = $val[1];
  142. else {
  143. $val = explode("/",$agent);
  144. $bd['version'] = $val[1];
  145. }
  146.  
  147. // test for Galeon
  148. }elseif(eregi("galeon",$agent)){
  149. $val = explode(" ",stristr($agent,"galeon"));
  150. $val = explode("/",$val[0]);
  151. $bd['browser'] = $val[0];
  152. $bd['version'] = $val[1];
  153.  
  154. // test for Konqueror
  155. }elseif(eregi("Konqueror",$agent)){
  156. $val = explode(" ",stristr($agent,"Konqueror"));
  157. $val = explode("/",$val[0]);
  158. $bd['browser'] = $val[0];
  159. $bd['version'] = $val[1];
  160.  
  161. // test for iCab
  162. }elseif(eregi("icab",$agent)){
  163. $val = explode(" ",stristr($agent,"icab"));
  164. $bd['browser'] = $val[0];
  165. $bd['version'] = $val[1];
  166.  
  167. // test for OmniWeb
  168. }elseif(eregi("omniweb",$agent)){
  169. $val = explode("/",stristr($agent,"omniweb"));
  170. $bd['browser'] = $val[0];
  171. $bd['version'] = $val[1];
  172.  
  173. // test for Phoenix
  174. }elseif(eregi("Phoenix", $agent)){
  175. $bd['browser'] = "Phoenix";
  176. $val = explode("/", stristr($agent,"Phoenix/"));
  177. $bd['version'] = $val[1];
  178.  
  179. // test for Firebird
  180. }elseif(eregi("firebird", $agent)){
  181. $bd['browser']="Firebird";
  182. $val = stristr($agent, "Firebird");
  183. $val = explode("/",$val);
  184. $bd['version'] = $val[1];
  185.  
  186. // test for Firefox
  187. }elseif(eregi("Firefox", $agent)){
  188. $bd['browser']="Firefox";
  189. $val = stristr($agent, "Firefox");
  190. $val = explode("/",$val);
  191. $bd['version'] = $val[1];
  192.  
  193. // test for Mozilla Alpha/Beta Versions
  194. }elseif(eregi("mozilla",$agent) &&
  195. eregi("rv:[0-9].[0-9][a-b]",$agent) && !eregi("netscape",$agent)){
  196. $bd['browser'] = "Mozilla";
  197. $val = explode(" ",stristr($agent,"rv:"));
  198. eregi("rv:[0-9].[0-9][a-b]",$agent,$val);
  199. $bd['version'] = str_replace("rv:","",$val[0]);
  200.  
  201. // test for Mozilla Stable Versions
  202. }elseif(eregi("mozilla",$agent) &&
  203. eregi("rv:[0-9]\.[0-9]",$agent) && !eregi("netscape",$agent)){
  204. $bd['browser'] = "Mozilla";
  205. $val = explode(" ",stristr($agent,"rv:"));
  206. eregi("rv:[0-9]\.[0-9]\.[0-9]",$agent,$val);
  207. $bd['version'] = str_replace("rv:","",$val[0]);
  208.  
  209. // test for Lynx & Amaya
  210. }elseif(eregi("libwww", $agent)){
  211. if (eregi("amaya", $agent)){
  212. $val = explode("/",stristr($agent,"amaya"));
  213. $bd['browser'] = "Amaya";
  214. $val = explode(" ", $val[1]);
  215. $bd['version'] = $val[0];
  216. } else {
  217. $val = explode("/",$agent);
  218. $bd['browser'] = "Lynx";
  219. $bd['version'] = $val[1];
  220. }
  221.  
  222. // test for Safari
  223. }elseif(eregi("safari", $agent)){
  224. $bd['browser'] = "Safari";
  225. $bd['version'] = "";
  226.  
  227. // remaining two tests are for Netscape
  228. }elseif(eregi("netscape",$agent)){
  229. $val = explode(" ",stristr($agent,"netscape"));
  230. $val = explode("/",$val[0]);
  231. $bd['browser'] = $val[0];
  232. $bd['version'] = $val[1];
  233. }elseif(eregi("mozilla",$agent) && !eregi("rv:[0-9]\.[0-9]\.[0-9]",$agent)){
  234. $val = explode(" ",stristr($agent,"mozilla"));
  235. $val = explode("/",$val[0]);
  236. $bd['browser'] = "Netscape";
  237. $bd['version'] = $val[1];
  238. }
  239.  
  240. // clean up extraneous garbage that may be in the name
  241. $bd['browser'] = ereg_replace("[^a-z,A-Z]", "", $bd['browser']);
  242. // clean up extraneous garbage that may be in the version
  243. $bd['version'] = ereg_replace("[^0-9,.,a-z,A-Z]", "", $bd['version']);
  244.  
  245. // check for AOL
  246. if (eregi("AOL", $agent)){
  247. $var = stristr($agent, "AOL");
  248. $var = explode(" ", $var);
  249. $bd['aol'] = ereg_replace("[^0-9,.,a-z,A-Z]", "", $var[1]);
  250. }
  251.  
  252. // finally assign our properties
  253. $this->Name = $bd['browser'];
  254. $this->Version = $bd['version'];
  255. $this->Platform = $bd['platform'];
  256. $this->AOL = $bd['aol'];
  257. }
  258. }

Report this snippet  

You need to login to post a comment.