我已经能够通过CodeIgniter检测用户正在使用的移动设备,但我无法检测当前移动设备正在运行的操作系统.
假设某人正在使用在Android上运行的三星移动设备,而另一个人则使用的是仍然是三星的普通Java移动操作系统.如何查看每个用户所在的操作系统?
Web*_*une 19
从http://mobiledetect.net下载库 将Mobile_Detect.php放入'libraries'
在主控制器内
public function index() {
$this -> load -> library('Mobile_Detect');
$detect = new Mobile_Detect();
if ($detect->isMobile() || $detect->isTablet() || $detect->isAndroidOS()) {
header("Location: ".$this->config->item('base_url')."/mobile"); exit;
}
}
Run Code Online (Sandbox Code Playgroud)
在http://dwij.co.in/mobile-os-detection-in-php-codeigniter上查找文档
小智 7
加载库。
$this->load->library('user_agent');
Run Code Online (Sandbox Code Playgroud)
使用此功能检测是否移动
$mobile=$this->agent->is_mobile();
if($mobile){
//your code
}
Run Code Online (Sandbox Code Playgroud)
我借用/窃取了这种从phpexcel codeigniter集成中加载类的方法。
从http://mobiledetect.net下载库,但将Mobile_Detect.php放在“ third_party”中,然后在“ libraries”中创建MobileDetect.php,并将以下代码放入其中:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH."third_party/Mobile_Detect.php";
class MobileDetect extends Mobile_Detect {
public function __construct() {
parent::__construct();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以像下面这样在控制器中使用它:
$this->load->library('MobileDetect');
if ($this->mobiledetect->isMobile()) {
//do something cool;
}
Run Code Online (Sandbox Code Playgroud)
我确信还有其他(甚至更好的)方法可以将mobiledetect集成到codeigniter中,我只是想分享我做的方法,希望对您有所帮助。
一些注意事项:
1)您不必使用存根文件MobileDetect.php,如果直接将Mobile_Detect.php放在“库”中,则仍可以使用它而$detect = new Mobile_Detect();无需调用如下函数:$this->mobile_detect->isMobile()
2)存根文件类的名称可以是您想要的任何名称,只要您遵循CodeIgniter准则即可。因此,例如,您可以使用“ MD”作为类名,然后使用$this->md->isMobile()
3)我建议if ( ! defined('BASEPATH')) exit('No direct script access allowed');在打开<?phpMobile_Detect.php 之后添加,以防止直接访问该类。