PHP CSS Browser Selector
Update
You can find a more feature rich version of the browser detector in my little PHP toolkit, called Kirby
Rafael Lima's CSS Browser Selector in PHP
CSS Browser Selector is a very smallExample
The background of this page will change on different os/browsers:
- yellow
- Internet Explorer
- orange
- Internet Explorer 7
- red
- Gecko Engine on Windows (Firefox, Mozilla, Camino)
- pink
- Gecko Engine on Linux (Firefox, Mozilla, Camino)
- gray
- Gecko Engine on Other OS (Firefox, Mozilla, Camino)
- green
- Opera
- blue
- Konqueror
- black
- Safari
- cyan
- Chrome
CSS of this example
.ie body {
background-color: yellow
}
.ie7 body {
background-color: orange
}
.gecko body {
background-color: gray
}
.win.gecko body {
background-color: red
}
.linux.gecko body {
background-color: pink
}
.opera body {
background-color: green
}
.konqueror body {
background-color: blue
}
.webkit body {
background-color: black
}
.chrome body {
background-color: cyan
}
Download
css_browser_selector.zip (4 kb)Usage
Include css_browser_selector.php in your app
<?php require("css_browser_selector.php"); ?>
…and add the following to your html template.
<html class="<?php echo css_browser_selector() ?>">
HTML-Output (for your browser):
<html class="">
Available OS Codes
- win - Microsoft Windows
- linux - Linux (x11 and linux)
- mac - Mac OS
- freebsd - FreeBSD
- ipod - iPod Touch
- iphone - iPhone
- webtv - WebTV
- mobile - J2ME Devices (ex: Opera mini)
Available Browser Codes
- ie - Internet Explorer (All versions)
- ie8 - Internet Explorer 8.x
- ie7 - Internet Explorer 7.x
- ie6 - Internet Explorer 6.x
- ie5 - Internet Explorer 5.x
- gecko - Mozilla, Firefox (all versions), Camino
- ff2 - Firefox 2
- ff3 - Firefox 3
- ff3_5 - Firefox 3.5 new
- opera - Opera (All versions)
- opera8 - Opera 8.x
- opera9 - Opera 9.x
- opera10 - Opera 10.x
- konqueror - Konqueror
- webkit or safari - Safari, NetNewsWire, OmniWeb, Shiira, Google Chrome
- safari3 - Safari 3.x
- chrome - Google Chrome
- iron - SRWare Iron new
PHP vs. JS
The advantage of this PHP version is that there's no additional request needed to include the javascript file and the selectors are available immediately.
The javascript version includes a client javascript detection with the js selector. This is not available in the PHP version.
Inspiration
As mentioned above this is just a port to PHP. The original javascript file has been created by Rafel Lima who's got the idea from a blog post by 37signals
License
Rafel has released his code under the following license and so do I: http://creativecommons.org/licenses/by/2.5/
Author
Bastian Allgeier bastian-allgeier.de
Follow me on Twitter twitter.com/bastianallgeier