PHP get_browser: how to identify ie7 versus ie6?

好久不见. 提交于 2019-12-09 23:03:08

问题


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


回答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.




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



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



回答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.




回答5:


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/



来源:https://stackoverflow.com/questions/1042176/php-get-browser-how-to-identify-ie7-versus-ie6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!