2013-05-31 263 views
6

我已经能够检测到用户使用CodeIgniter的移动设备,但是我一直无法检测到当前移动设备正在运行的操作系统。CodeIgniter移动操作系统检测

假设有人正在使用在Android上运行的三星移动设备,另一个使用的是仍然是三星的普通Java移动操作系统。我如何检查每个用户所在的操作系统?从http://mobiledetect.net

回答

17

下载库将Mobile_Detect.php到“库”

内主控制器

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; 
    } 
} 

找到文档上http://dwij.co.in/mobile-os-detection-in-php-codeigniter

+0

得益于它完美! –

0

如果使用会话类有一个变量内置于user_agent

4

我借用/ s容忍这种从phpexcel codeigniter集成中加载类的方法。从http://mobiledetect.net

下载库,而是把Mobile_Detect.php在“THIRD_PARTY”然后创建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(); 
    } 
} 

现在你可以在你的控制器使用像这样:

$this->load->library('MobileDetect'); 
if ($this->mobiledetect->isMobile()) { 
    //do something cool; 
} 

我敢肯定还有其他的(甚至更好)的方式来mobiledetect融入笨,我只是想分享我所采取的方式,我希望这是有帮助的。

有两点要注意:

1)您没有使用存根文件MobileDetect.php,如果你把Mobile_Detect.php直接在“库”,你仍然可以使用它没有$detect = new Mobile_Detect();改为调用函数喜欢这个:$this->mobile_detect->isMobile()

2)只要你遵循CodeIgniter的指导方针,存根文件类的名称可以是任何你想要的。因此,例如,你可以使用“MD”作为类名,然后用$this->md->isMobile()

3)我建议Mobile_Detect.php开幕<?php后加入if (! defined('BASEPATH')) exit('No direct script access allowed');以防止类直接访问引用它。从https://github.com/serbanghita/Mobile-Detect 复制Mobile_Detect.php

1

下载库到THIRD_PARTY目录 CI中创建一个辅助类 //这个函数将返回用户代理手机或平板电脑或计算机

if(!function_exists('is_MTC')): 
    function is_MTC() 
    { 
     require(APPPATH .'third_party/Mobile_Detect.php'); 
     $detect = new Mobile_Detect; 
     return ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer'); 
    } 
endif; 

在视图您可以直接调用is_MTC功能,并检查用户代理
//这将打印用户代理

<?php echo is_MTC(); ?> 

了解更多关于codeigniter helper功能https://ellislab.com/codeigniter/user-guide/general/helpers.html

2

加载lib。

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

使用此功能可以检测为移动

$mobile=$this->agent->is_mobile(); 
if($mobile){ 
    //your code 
} 
0

笨已经内置支持browser or agent detection in codeigniter

内部控制器使用下面的示例代码:

$this->load->library('user_agent'); 
if ($this->agent->is_mobile()) { 
    // Is a mobile browser 
} else { 
    // Is a Desktop/Bot User Agent 
}