Code Igniter - 检测浏览器的最佳方式

时间:2012-04-20 07:55:52

标签: php codeigniter

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

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

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

2 个答案:

答案 0 :(得分:16)

在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->uri->segment(1) != 'unsupported-browser'作为额外条件以避免重定向循环。

答案 1 :(得分:2)

http://mobiledetect.net下载库

将Mobile_Detect.php放入'library'

在主控制器内

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

查找有关http://dwij.co.in/mobile-os-detection-in-php-codeigniter

的文档