2013-05-31 252 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 
}