代码之家  ›  专栏  ›  技术社区  ›  montooner

php-get-browser:如何识别IE7和IE6?

  •  2
  • montooner  · 技术社区  · 15 年前

    有没有办法用php的get_browser()函数区分ie7和ie6?

    5 回复  |  直到 12 年前
        1
  •  27
  •   Andrew Moore    15 年前

    您可以这样做:

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

    快速看一下这位官员 get_browser() 文件会回答你的问题。 务必先阅读文档。

        2
  •  4
  •   Jedidiah    12 年前

    我读到get_browser()是一个相对较慢的函数,所以我在寻找更快的函数。此代码检查msie 7.0,输出“otay!”如果属实。这与上一篇文章的答案基本相同,只是更简洁而已。相当简单的if语句:

    <?php 
    if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 7.0'))
        echo 'Otay!';
    ?>
    
        3
  •  3
  •   Fahim Parkar    12 年前

    下面是一个完整的例子 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
  •  2
  •   Simon Scarfe    15 年前

    如果您的逻辑是决定要包含什么样式表或脚本,那么可能值得使用条件注释的HTML路径:

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

    这样您就可以绕过任何自定义浏览器字符串等。更多信息 QuirksMode .

        5
  •  0
  •   Marcy Sutton    15 年前

    我为php ie6条件找到了一个不同的、非常简单的解决方案,我可以为自己的目的进行编辑:

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

    完整的脚本可以在这里找到:

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