Possible Duplicate:
Determine Browser’s Version
I want to determine the browser type in the server code.
As it is, I am spending too much time hacking the HTML/CSS to get the site to display consistently across all browsers. The only browser in which the site is rendered differently (incorrectly), is IE, so I want to notify the users so that they understand why the pages are not as they would expect.
To this end, I am only interested in detecting requests from IE, so that I can inform IE users that the site is best viewed using another web browser.
I believe that the browser type is sent in one of the HTTP headers as part of the GET request. I am thinking of using this in my logic. Is there a better way?
A code snippet will be very useful. something along the lines of:
<?php
$flag = $_GET(SOME_VARIABLE); // check if it is ANY IE version
?>
You can take a look on $_SERVER['HTTP_USER_AGENT'] :)
The internet is full of functions for browser detection
function detect_ie(){
if (isset($_SERVER['HTTP_USER_AGENT']) &&
(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)){
return true;
}else{
return false;
}
}
PHP's get_browser
function should be perfect for you -
get_browser - Tells what the user's browser is capable of
return values - The information is returned in an object or an array which will contain various data elements representing, for instance, the browser's major and minor version numbers and ID string; TRUE/FALSE values for features such as frames, JavaScript, and cookies; and so forth.
$browser = get_browser(null, true);
This function can return an array containing much information about the users browser
Array
(
...
[platform] => WinXP
[browser] => Firefox
[version] => 0.9
[majorver] => 0
[minorver] => 9
...
)
if you wanna be more specific you can try.
function getBrowser()
{
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = 'Unknown';
$platform = 'Unknown';
$version= "";
if (preg_match('/linux/i', $u_agent))
{
$platform = 'linux';
}
elseif (preg_match('/macintosh|mac os x/i', $u_agent))
{
$platform = 'mac';
}
elseif (preg_match('/windows|win32/i', $u_agent))
{
$platform = 'windows';
}
if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
{
$bname = 'Internet Explorer';
$ub = "MSIE";
}
elseif(preg_match('/Firefox/i',$u_agent))
{
$bname = 'Mozilla Firefox';
$ub = "Firefox";
}
elseif(preg_match('/Chrome/i',$u_agent))
{
$bname = 'Google Chrome';
$ub = "Chrome";
}
elseif(preg_match('/Safari/i',$u_agent))
{
$bname = 'Apple Safari';
$ub = "Safari";
}
elseif(preg_match('/Opera/i',$u_agent))
{
$bname = 'Opera';
$ub = "Opera";
}
elseif(preg_match('/Netscape/i',$u_agent))
{
$bname = 'Netscape';
$ub = "Netscape";
}
$known = array('Version', $ub, 'other');
$pattern = '#(?' . join('|', $known) .')[/ ]+(?[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $u_agent, $matches))
{
}
$i = count($matches['browser']);
if ($i != 1)
{
if (strripos($u_agent,"Version") $u_agent,
'name' => $bname,
'version' => $version,
'platform' => $platform,
'pattern' => $pattern
);
}
this will do the trick, but you should also just do it client side with JavaScript.