如何在Codeigniter3.0.6中使用用户代理?

时间:2016-05-27 14:34:47

标签: php codeigniter

我在用户代理上使用Codeigniter3.0.6测试如下功能然后我尝试测试IPhone,Chrome检查设备模式但我只得到1号。 我想查看我是否在手机中查看此网站,它将回显2号 如果在PC浏览器echo number1中,如果无法检测到浏览器显示0。

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Main_Controller extends MY_Controller
{

    public $data = array();

    public function __construct()
    {
        parent::__construct();

        var_dump($this->CheckDevices());
        exit();

        $this->data['deviceType'] = $this->CheckDevices();
        if ($this->data['deviceType'] == 1) {
            $this->set_navigation();
        }
        elseif ($this->data['deviceType'] == 2) {
            return false;
        }elseif($this->data['deviceType'] == 0){
            return false;
        }
    }

    private function CheckDevices()
    {
        $this->load->library('user_agent');
        $agent = '';
        if ($this->agent->is_browser()) {

            $agent = 1;

        } elseif ($this->agent->is_mobile()) {

            $agent = 2;

        } else {

            $agent = 0;

        }
        return $agent;
    }

    private function set_navigation()
    {
        $this->load->library('session');
        $this->load->library("nav_libs");
        return $this->data['menus'] = $this->nav_libs->navigation();
    }
}

?>

1 个答案:

答案 0 :(得分:1)

你可以试试这个 应用/核心/ My_Core.php

class My_Core extends CI_Controller{
    public function __construct(){
        parent::__construct();
    }

    public function detect_dvice(){
        $this->load->library('user_agent');

        if( $this->agent->is_mobile()){
            $_is_mobile = 1;
        }
        else{
            $_is_mobile = 2;
        }

        return $_is_mobile
    }
}

普通控制器 应用/控制器/ Test_controller.php

class Test_controller extends My_Core{
    public function __construct(){
        parent::__construct();
    }

    public function index(){
        echo $this->detect_dvice();        
    }
}