Question

Is there any way to differentiate IE7 versus IE6 using PHP's get_browser() function?

Was it helpful?

Solution

You can do so as such:

$browser = get_browser();

if($browser->browser == 'IE' && $browser->majorver == 6) {
    echo "IE6";
} elseif($browser->browser == 'IE' && $browser->majorver == 7) {
    echo "IE7";
}

A quick look to the official get_browser() documentation would of answered your question. Always read the documentation before.

OTHER TIPS

I read that get_browser() is a relatively slow function, so I was looking for something speedier. This code checks for MSIE 7.0, outputting "Otay!" if true. It's basically the same answer as the previous post, just more concise. Fairly straightforward if statement:

<?php 
if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 7.0'))
    echo 'Otay!';
?>

Below is a complete example taken from here.

$browser = get_browser();

switch ($browser->browser) {
    case "IE":
        switch ($browser->majorver) {
            case 7:
                echo '<link href="ie7.css" rel="stylesheet" type="text/css" />';
                break;
            case 6:
            case 5:
                echo '<link href="ie5plus.css" rel="stylesheet" type="text/css" />';
                break;
            default:
                echo '<link href="ieold.css" rel="stylesheet" type="text/css" />';
        }

        break;

    case "Firefox":
    case "Mozilla":
        echo '<link href="gecko.css" rel="stylesheet" type="text/css" />';
        break;

    case "Netscape":
        if ($browser->majorver < 5) {
            echo '<link href="nsold.css" rel="stylesheet" type="text/css" />';
        } else {
            echo '<link href="gecko.css" rel="stylesheet" type="text/css" />';
        }
        break;

    case "Safari":
    case "Konqueror":
        echo '<link href="gecko.css" rel="stylesheet" type="text/css" />';
        break;

    case "Opera":
        echo '<link href="opera.css" rel="stylesheet" type="text/css" />';
        break;

    default:
        echo '<link href="unknown.css" rel="stylesheet" type="text/css" />';
}

If your logic is to decide what stylesheets or scripts to include, it maybe worth going the HTML route of conditional comments:

<!--[if IE 6]>
According to the conditional comment this is Internet Explorer 6<br />
<![endif]-->
<!--[if IE 7]>
According to the conditional comment this is Internet Explorer 7<br />
<![endif]-->

That way you get around any custom browser strings and the like. More info at QuirksMode.

I found a different, really simple solution for a PHP IE6 conditional that I was able to edit for my own purposes:

<?php  

// IE6 string from user_agent  
 $ie6 = "MSIE 6.0";  

// detect browser  
 $browser = $_SERVER['HTTP_USER_AGENT'];  

 // yank the version from the string  
 $browser = substr("$browser", 25, 8);  

 // if IE6 set the $alert   
 if($browser == $ie6){ 
      // put your code here    
 }  
 ?>  

The full script can be found here:

http://www.thatgrafix.com/php_detect/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top