Browser Detection
We've all been there. We need to serve pages differently depending on browser and version. There's the messy "IF" statements for "conditional CSS" but what if we want something a little cleaner?
Enter the PHP user agent request. It's important to note right away that this can be defeated by some browsers which allow users to spoof another browser/version. If that's important for your needs then this method is not 100% reliable, but for most of us in most cases this will work just fine.
Here's the code:
<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match('|MSIE ([0-9].[0-9]{1,2})|',$useragent,$matched)) {
$browser_version=$matched[1];
$browser = 'IE';
} elseif (preg_match( '|Opera ([0-9].[0-9]{1,2})|',$useragent,$matched)) {
$browser_version=$matched[1];
$browser = 'Opera';
} elseif(preg_match('|Firefox/([0-9\.]+)|',$useragent,$matched)) {
$browser_version=$matched[1];
$browser = 'Firefox';
} elseif(preg_match('|Safari/([0-9\.]+)|',$useragent,$matched)) {
$browser_version=$matched[1];
$browser = 'Safari';
} else {
// browser not recognized!
$browser_version = 0;
$browser= 'other';
}
echo "This browser reported itself as \"".$browser." ".$browser_version."\".<br />This browser type and version will determine what (if anything) you see below:<br /><hr /><br />";
if($browser=="IE"){
$numeric_browser_version=$browser_version;
$numeric_browser_version=str_replace(".","",$numeric_browser_version);
if($numeric_browser_version>=80 && $numeric_browser_version<90){
echo "This signifies an IE browser version of 8.xx. The appropriate include for CSS would be placed here instead of this \"echo\" statement.<br />";
}
if($numeric_browser_version<=80){
echo "This signifies an IE browser version of 8.0 or less. The appropriate include for CSS would be placed here instead of this \"echo\" statement.<br />";
}
if($numeric_browser_version<=70){
echo "This signifies an IE browser version of 7.0 or less. The appropriate include for CSS would be placed here instead of this \"echo\" statement.<br />";
}
if($numeric_browser_version<=60){
echo "This signifies an IE browser version of 6.0 or less. The appropriate include for CSS would be placed here instead of this \"echo\" statement.<br />";
}
if($numeric_browser_version<=55){
echo "This signifies an IE browser version of 5.5 or less. The appropriate include for CSS would be placed here instead of this \"echo\" statement.<br />";
}
}
else if($browser=="Firefox"){
echo "Hello, firefox user! The appropriate include for CSS (if applicable) would be placed here instead of this \"echo\" statement.";
}
?>
This code is set up to display very minimal text but should accurately show what your browser is reporting itself to be (as mentioned earlier some browsers allow users to spoof so this is not an air-tight method). Depending on which browser you use to view the page you will see a different message. All Firefox users, regardless of version number, will see the "Hello, firefox user!" statement. IE users will see one or more statements depending on what version number they are running. IE 8.xx users will see all of the above, while if an IE 4.xx user somehow wandered in they would only see the "5.5 or less" statement.
You can view the output of this example script here.