Posted By

frakim on 05/21/08


Tagged

css php textmate browser override


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

mikegreen
iroybot
nerdfiles
umang_nine


browser.php


 / Published in: CSS
 

URL: http://minimaldesign.net/downloads/tools/css-override

For the why and the what, make sure you check out my blog article Never hack your CSS files again, you’ll get a much better idea what this is for. update: I added support for the iPhone in version 1.0.2. More info in my blog post about serving iPhone specific CSS. Requirements A website hosted on a server that has PHP Basic knowledge of FTP, HTML, and CSS Installation Download, unzip, and open in a text editor Modify line 22 ($cssdir = '/css/';) to point to your CSS files on your server. The default points to a folder called “_css” located at the root of your website, where your index file is Upload to your server Create CSS files inside the CSS folder defined in step 2 (names available) Add this code in your HTML (make sure you put it under the main CSS)

That’s it… You’re done!

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

Report this snippet  

You need to login to post a comment.