PHP get_browser: how to identify ie7 versus ie6?

18,282

Solution 1

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.

Solution 2

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!';
?>

Solution 3

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" />';
}

Solution 4

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.

Share:
18,282
montooner
Author by

montooner

I want the autobiographer badge.

Updated on June 04, 2022

Comments

  • montooner
    montooner almost 2 years

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