Codeigniter user_agent无法正常工作

时间:2016-02-02 09:41:26

标签: php android codeigniter mobile

我已使用以下内容检测移动设备,机器人和桌面,如codeigniter的用户文档中所述。

     if ($this->agent->is_mobile()) {
           // This is a mobile device
        $data['is_mobile'] = true;
        $this->load->view('mobile/template/header');
        $this->load->view('mobile/template/imageDiv', $data);
        $this->load->view('mobile/template/footer');       
        } else if ($this->agent->is_robot()) {
            // This is a robot, treat it like a mobile device so that our content is indexed
        $data['is_mobile'] = true;
        $this->load->view('mobile/template/header');
        $this->load->view('mobile/template/imageDiv', $data);
        $this->load->view('mobile/template/footer');       

        } else {
        $data['is_mobile'] = false;
        $this->load->view('template/header');

        $this->load->view('login/loginOnHover', $data);
        $this->load->view('template/imageDiv', $data);
        $this->load->view('template/booking_track', $data);
        $this->load->view('template/recent_hotels', $data);
        $this->load->view('template/easybooking');
        $this->load->view('template/features');
        $this->load->view('template/stepsApi', $data);
        $this->load->view('template/footer');       
    }

它适用于几乎所有移动设备,但对于谷歌nexus系列,黑莓PlayBook和Ipads它显示桌面视图。 我该怎么解决?

1 个答案:

答案 0 :(得分:1)

尝试这种方式: -

在application / libraries / MY_User_agent.php中:

class MY_User_agent extends CI_User_agent {

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

    public function is_ipad()
    {
        return (bool) strpos($_SERVER['HTTP_USER_AGENT'],'iPad');
          // You can add other checks for other tablets
    }
}

通过这种方式检查: -

    $this->load->library('user_agent');

    ($this->agent->is_ipad() === TRUE) ? $is_ipad = "Yes" : $is_ipad = "No";

    echo "Using ipad: $is_ipad";