PHP: If internet explorer 6, 7, 8 , or 9

PhpBrowser

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

This is what I ended up using a variation of, which checks for IE8 and below:

if (preg_match('/MSIE\s(?P<v>\d+)/i', @$_SERVER['HTTP_USER_AGENT'], $B) && $B['v'] <= 8) {
    // Browsers IE 8 and below
} else {
    // All other browsers
}

Solution 2 - Php

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
  }
}

Solution 3 - Php

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.

Solution 4 - Php

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
}

Solution 5 - Php

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
}

Solution 6 - Php

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.

Solution 7 - Php

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

Solution 8 - Php

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.

Solution 9 - Php

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;
  
}

Solution 10 - Php

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);

Solution 11 - Php

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.

Solution 12 - Php

> '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.

Solution 13 - Php

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]-->

Solution 14 - Php

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

Solution 15 - Php

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 :-(
    }

Solution 16 - Php

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'])

Solution 17 - Php

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; 
} 
?>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCameronView Question on Stackoverflow
Solution 1 - PhpCameronView Answer on Stackoverflow
Solution 2 - PhpDougView Answer on Stackoverflow
Solution 3 - PhpDaffView Answer on Stackoverflow
Solution 4 - PhpMichael JasperView Answer on Stackoverflow
Solution 5 - PhpVince LoweView Answer on Stackoverflow
Solution 6 - PhpMike SView Answer on Stackoverflow
Solution 7 - PhpxsorView Answer on Stackoverflow
Solution 8 - PhptomsseisumsView Answer on Stackoverflow
Solution 9 - PhpminorgodView Answer on Stackoverflow
Solution 10 - PhpMarkus ZellerView Answer on Stackoverflow
Solution 11 - PhpUku LoskitView Answer on Stackoverflow
Solution 12 - PhparneheheView Answer on Stackoverflow
Solution 13 - PhpEnrico TempestiView Answer on Stackoverflow
Solution 14 - PhpantoniView Answer on Stackoverflow
Solution 15 - PhpVladoView Answer on Stackoverflow
Solution 16 - PhpIvan Mendizabal SaraviaView Answer on Stackoverflow
Solution 17 - Phpsajin tmView Answer on Stackoverflow