[php] PHP: If internet explorer 6, 7, 8 , or 9

I want to do a conditional in PHP for the different versions of Internet Explorer along the lines of:

if($browser == ie6){ //do this} elseif($browser == ie7) { //dothis } elseif...

I have seen many variations on similar code, but looking for something super simple that is very easy to code to do some simple if and else and do different things.

Thanks

EDIT: I need this to show some different messages to users so CSS conditionals etc are no good.

This question is related to php browser

The answer is


You could use something like this for different messages or div/css

<!--[if IE 6]>
<style type="text/css">
div.ie6 { display:block; }
</style>
<![endif]-->

<!--[if IE 7]>
<style type="text/css">
div.ie7 { display:block; }
</style>
<![endif]-->

<!--[if IE 8]>
<style type="text/css">
div.ie8 { display:block; }
</style>
<![endif]-->

<!--[if IE 9]>
message1
<![endif]-->

<!--[if !IE 6]>
message2
<![endif]-->

<!--[if lt IE 8]>
message3
<![endif]-->

OR use different div of css

<!--[if lte IE 8]>
<style type="text/css">
div.lteie8 { display:block; }
</style>
<![endif]-->

<!--[if gt IE 6]>
<style type="text/css">
div.gtie6 { display:block; }
</style>
<![endif]-->

<!--[if gte IE 6]>
<style type="text/css">
div.gteie6 { display:block; }
</style>
<![endif]-->

Checking for MSIE only is not enough to detect IE. You need also "Trident" which is only used in IE11. So here is my solution which worked an versions 8 to 11.

$agent=strtoupper($_SERVER['HTTP_USER_AGENT']);
$isIE=(strpos($agent,'MSIE')!==false || strpos($agent,'TRIDENT')!==false);

'HTTP_USER_AGENT' Contents of the User-Agent: header from the current request, if there is one. This is a string denoting the user agent being which is accessing the page. A typical example is: Mozilla/4.5 [en] (X11; U; Linux 2.2.9 i586). Among other things, you can use this value with get_browser() to tailor your page's output to the capabilities of the user agent.

So I assume you'll be able to get the browser name/id from the $_SERVER["HTTP_USER_AGENT"] variable.


if you have Internet Explorer 11 and it's running over a touch screen pc, you must use: preg_match('/Trident/7.0; Touch; rv:11.0/', $_SERVER['HTTP_USER_AGENT']) instead of: preg_match('/Trident/7.0; rv:11.0/', $_SERVER['HTTP_USER_AGENT'])


but still useful - and works with IE11 ! here is another short way to get the common browsers returned for css class:

function get_browser()
{
    $browser = '';
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    if (preg_match('~(?:msie ?|trident.+?; ?rv: ?)(\d+)~', $ua, $matches)) $browser = 'ie ie'.$matches[1];
    elseif (preg_match('~(safari|chrome|firefox)~', $ua, $matches)) $browser = $matches[1];

    return $browser;
}

So this function returns: 'safari' or 'firefox' or 'chrome', or 'ie ie8', 'ie ie9', 'ie ie10', 'ie ie11'.

hope it helps


Here is a great resource for detecting browsers in php: http://php.net/manual/en/function.get-browser.php

Here is one of the examples that seems the simplest:

<?php
function get_user_browser()
{
    $u_agent = $_SERVER['HTTP_USER_AGENT'];
    $ub = '';
    if(preg_match('/MSIE/i',$u_agent))
    {
        $ub = "ie";
    }
    elseif(preg_match('/Firefox/i',$u_agent))
    {
        $ub = "firefox";
    }
    elseif(preg_match('/Safari/i',$u_agent))
    {
        $ub = "safari";
    }
    elseif(preg_match('/Chrome/i',$u_agent))
    {
        $ub = "chrome";
    }
    elseif(preg_match('/Flock/i',$u_agent))
    {
        $ub = "flock";
    }
    elseif(preg_match('/Opera/i',$u_agent))
    {
        $ub = "opera";
    }

    return $ub;
}
?>

Then later in your code you could say something like

$browser = get_user_browser();
if($browser == "ie"){
    //do stuff
}

if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/(?i)msie|trident|edge/",$_SERVER['HTTP_USER_AGENT'])) {
// eh, IE found
}

A tridend based approach would be better. Here is a quick function for checking IE 8.

<?php
function is_IE8(){
   if(strpos(str_replace(' ', '', $_SERVER['HTTP_USER_AGENT']),'Trident/4.0')!== FALSE){
       return TRUE;
   };
   return FALSE; 
} 
?>

Notice the case in 'Trident':

if (isset($_SERVER['HTTP_USER_AGENT']) &&
    ((strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) || strpos($_SERVER['HTTP_USER_AGENT'], 'Trident') !== false)) {   
     // IE is here :-(
    }

You can check the HTTP_USER_AGENT server variable. The user agent transfered by IE contains MSIE

if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) { ... }

For specific versions you can extend your condition

if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.') !== false) { ... }

Also see this related question.


You can do this via parsing the user-agent header:

http://php.about.com/od/learnphp/p/http_user_agent.htm

Be wary that this is not very reliable and can be trivially spoofed.


PHP has a function called get_browser() that will return an object (or array if you so choose) with details about the users' browser and what it can do.

A simple look through gave me this code:

$browser = get_browser( null, true );
if( $browser['name'] == "Firefox" )
    if( $browser['majorversion'] == 4 )
        echo "You're using Firefox version 4!";

This is not a surefire way (as it reads from HTTP_USER_AGENT, which can be spoofed, and will sometimes be analyzed wrong by php), but it's the easiest one that you can find as far as I know.


I do this

$u = $_SERVER['HTTP_USER_AGENT'];

$isIE7  = (bool)preg_match('/msie 7./i', $u );
$isIE8  = (bool)preg_match('/msie 8./i', $u );
$isIE9  = (bool)preg_match('/msie 9./i', $u );
$isIE10 = (bool)preg_match('/msie 10./i', $u );

if ($isIE9) {
    //do ie9 stuff
}

You can as well look into PHP's get_browser(); http://php.net/manual/en/function.get-browser.php

Maybe you'll find it useful for more features.


Here's a little php function I wrote that uses the regex directly from MSFT's suggested javascript sniffing code from this article: http://msdn.microsoft.com/en-us/library/ms537509(v=vs.85).aspx

/**
* Returns the version of Internet Explorer or false
*/
function isIE(){

    $isIE = preg_match("/MSIE ([0-9]{1,}[\.0-9]{0,})/",$_SERVER['HTTP_USER_AGENT'],$version);
    if($isIE){
        return $version[1];
    }
    return $isIE;

}

A version that will continue to work with both IE10 and IE11:

preg_match('/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT'], $matches);
if(count($matches)<2){
  preg_match('/Trident\/\d{1,2}.\d{1,2}; rv:([0-9]*)/', $_SERVER['HTTP_USER_AGENT'], $matches);
}

if (count($matches)>1){
  //Then we're using IE
  $version = $matches[1];

  switch(true){
    case ($version<=8):
      //IE 8 or under!
      break;

    case ($version==9 || $version==10):
      //IE9 & IE10!
      break;

    case ($version==11):
      //Version 11!
      break;

    default:
      //You get the idea
  }
}