【问题标题】:Code Igniter - Best way to detect browserCodeigniter - 检测浏览器的最佳方法
【发布时间】:2012-04-20 07:55:51
【问题描述】:

我想将特定年龄的所有浏览器分流到他们自己的页面。这样做的最佳方法是什么?也许标题中包含一些JS:

 <!--[if lte IE 7 ]>
    <script type="text/javascript">
        window.location = "/unsupported-browser/";
    </script>
    <![endif]-->

以上不应该将浏览器发送到:http://example.com/unsupported-browser/ 我有一个基本的控制器和视图来处理它吗?就这么简单吗?

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    改为在 php 中执行此操作。在该页面使用user_agent classredirect

    但更重要的是,为什么不允许 IE 用户访问您的网站?是因为 CSS 还是其他原因?

    代码:

    $this->load->helper('url');
    $this->load->library('user_agent');
    
    if ($this->agent->browser() == 'Internet Explorer' and $this->agent->version() <= 7)
        redirect('/unsupported-browser');
    

    编辑:

    如前所述;如果您想在整个网站上使用它,请在 MY_Controller 中运行它,并确保添加 $this-&gt;uri-&gt;segment(1) != 'unsupported-browser' 作为额外条件以避免重定向循环。

    【讨论】:

    • OP 说转移到不同的页面不是拒绝访问,但是 user_agent 是 id 所做的
    • 根据示例,它们似乎会被发送到一个页面,上面写着“此站点在您的浏览器中不可用,请升级它”
    • 是的,你是对的,这不是一件好事,但我经常受到诱惑
    • 是的,我听到了。如果每个人都使用最新的 mozilla 和 webkit 浏览器,我会更喜欢,这样我总是知道我的用户因此获得了什么。这一切都取决于您希望您的服务能够正常运行的互联网上有多少人。例如,想想 Facebook 是否有 /unsupported-browser 页面:)
    • 由于某种原因,此重定向总是转到:mysite.com/?/unsupported-browser 知道为什么吗?正在添加到路径中?
    【解决方案2】:

    http://mobiledetect.net下载库

    将 Mobile_Detect.php 放入“库”

    主控制器内部

    public function index() {
        $this -> load -> library('Mobile_Detect');
        $detect = new Mobile_Detect();
        if ($detect->is('Chrome') || $detect->is('iOS')) {
            // whatever you wanna do here.
        }
    }
    

    https://dwij.net/mobile-os-detection-in-php-codeigniter 上查找文档

    【讨论】:

      猜你喜欢
      • 2011-12-03
      • 2011-06-16
      • 2010-10-09
      • 2011-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-18
      • 2012-09-11
      相关资源
      最近更新 更多