PHP get_browser:如何识别 ie7 和 ie6?

发布于 2024-07-25 14:52:16 字数 51 浏览 8 评论 0原文

有没有办法使用 PHP 的 get_browser() 函数来区分 IE7 和 IE6?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

狠疯拽 2024-08-01 14:52:17

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

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

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!';
?>
微凉 2024-08-01 14:52:17

以下是取自此处的完整示例。

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

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" />';
}
眼前雾蒙蒙 2024-08-01 14:52:17

如果您的逻辑是决定要包含哪些样式表或脚本,那么可能值得采用条件注释的 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

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.

尬尬 2024-08-01 14:52:17

我发现了一个不同的、非常简单的 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/

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/

深海夜未眠 2024-08-01 14:52:16

您可以这样做:

$browser = get_browser();

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

快速查看官方 get_browser () 文档会回答您的问题。 务必先阅读文档。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文