繁体   English   中英

为什么我的代码在本地主机上的Internet Explorer版本8上不能在服务器上运行,而在Internet Explorer版本10上却不能运行

[英]Why does my Code work on Internet explorer version 8 on localhost not on server, but not on internet explorer version 10

我有一个代码可以在本地主机上的Internet Explorer版本8上运行。它不能在服务器(Filezilla)上运行。同样的代码也不能在本地服务器上的Internet Explorer版本10上运行(也可以在服务器(Filezilla)上运行)。我想要解决方案以便它可以在本地主机和服务器上的IE版本10上运行。代码如下。

<?php
    function get_user_browser() {
        $u_agent = $_SERVER['HTTP_USER_AGENT'];
        $ub = '';
        if (preg_match('/MSIE/i', $u_agent)) {
            $ub = "ie";
        } elseif (preg_match('/Firefox/i', $u_agent)) {
            $ub = "firefox";
        } elseif (preg_match('/Safari/i', $u_agent)) {
            $ub = "safari";
        } elseif (preg_match('/Chrome/i', $u_agent)) {
            $ub = "chrome";
        } elseif (preg_match('/Flock/i', $u_agent)) {
            $ub = "flock";
        } elseif (preg_match('/Opera/i', $u_agent)) {
            $ub = "opera";
        }

        return $ub;
    }

    //echo get_user_browser();
    $browser = get_user_browser();
    if ($browser == 'ie') {
        echo "<script>alert('Your Browser is not compatible, Please Update the version')</script>";
        echo "<script>window.open('www.google.com','_self')</script>";
    }
?>

如今,检测IE是一件非常琐碎的事情,不需要那么细的锤子。 当然,您可以像这样在后端进行检查。 但是,让我解释一下为什么它不是最佳方法。

客户端每次请求页面时,都需要您花费一些时间通过正则表达式来处理浏览器名称,而他自己可以轻松地对其进行检查。 他是在计算机上运行浏览器的人。

我建议您请用户检查页面是否针对其浏览器进行了优化。 现在,为什么可行? 如果用户诱骗您以为他们使用的不是浏览器,那么唯一的受害者就是他们。 这样,您可以节省免费的处理时间。 免费的东西不是很棒吗?

可以通过仅使用HTML,CSS和一个框来解决此问题:

的CSS

#incompatibleBrowser { display:none; }

的HTML

<div id="incompatibleBrowser">Get a better browser! [link]</div>
<!--[if IE]>
    <link rel="stylesheet" type="text/css" href="ie.css" />
<![endif]-->

在这里, ie.css将包含:

的CSS

#incompatibleBrowser { display:block; }
// other rules that you might find appropriate for IE users

您可以使用内置函数get_browser()

链接: http//php.net/manual/de/function.get-browser.php

(请注意在目录中包含实际的browsercap.ini-您将在上面的链接中找到它)。

但是首先,您应该检查浏览器提供了哪些字符串,因为许多插件允许您的浏览器模仿(伪造)蜜蜂其他浏览器。 这将帮助您快速分析问题。

正如您在此处看到的那样: http : //www.useragentstring.com/pages/Browserlist/ ,将MSIE作为检查参数不是最佳方法,至少如果您将其放在检查例程中的话,至少不是最好的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM