views:

3512

answers:

5

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

+9  A: 

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.

Andrew Moore
You Beat me to it!
micmoo
Do you need == in your comparsion to 'IE'?
Robert Gowland
@Robert: Yes, I have corrected the typo.
Andrew Moore
+2  A: 

Here's a complete example taken from http://www.tuxradar.com/practicalphp/16/5/0

$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" />';
}
micmoo
+1  A: 

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.

Simon Scarfe
A: 

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/

Marcy Sutton
A: 

There are 2 bugs in the first code.

Vlatko Šurlan