2013-05-31 14 views
6

CodeIgniterを使用しているモバイルデバイスを検出できましたが、現在のモバイルデバイスが実行されているオペレーティングシステムを検出できませんでした。CodeIgniterモバイルOS検出

Androidで動作するSamsungモバイルデバイスを使用している人と、まだSamsungである通常のJavaモバイルオペレーティングシステムを使用している人がいるとします。各ユーザーがどのオペレーティングシステムを使用しているかを確認するにはどうすればよいですか?

答えて

17

http://mobiledetect.net からダウンロードライブラリは、「ライブラリのメインコントローラ内部の

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

にしてMobile_Detect.phpを入れて、あなたがセッションクラスを使用する場合、変数があるhttp://dwij.co.in/mobile-os-detection-in-php-codeigniter

+0

感謝を! –

0

のドキュメントを探します右に組み込まれています。user_agent

4

私は借りた/ s phpexcel codeigniterの統合からこのクラスをロードするこの方法をお勧めします。

http://mobiledetect.netからダウンロードライブラリに、しかしその後、「ライブラリ」にMobileDetect.phpを作成する「THIRD_PARTY」でMobile_Detect.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; 
} 

私はCodeIgniterのにmobiledetectを統合するために、他の(より良い)方法があると確信している、私はちょうど私がやった方法を共有したいと思った、私はそれが参考に願っています。

ノートのカップル:

1)あなたは、スタブファイルMobileDetect.phpを使用する必要はありません、あなたは「ライブラリ」に直接Mobile_Detect.phpを置けばあなたはまだ呼び出す代わりに$detect = new Mobile_Detect();せずにそれを使用することができますfunctionsは次のようになりました:$this->mobile_detect->isMobile()

2)スタブファイルクラスの名前は、CodeIgniterのガイドラインに従っている限り、何でも構いません。だから、例えば、あなたはクラス名として「MD」を使用することができ、その後、私はクラスへの直接アクセスを防止するためにMobile_Detect.phpの開口部<?phpif (! defined('BASEPATH')) exit('No direct script access allowed');を追加することをお勧めします$this->md->isMobile()

3)とそれを参照します。 THIRD_PARTYディレクトリ にhttps://github.com/serbanghita/Mobile-Detect コピーMobile_Detect.phpから

1

ダウンロードライブラリは、CodeIgniterの中でヘルパークラスを作成します //ビューでユーザエージェントの携帯電話やタブレットやパソコン

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(); ?> 
を印刷します0

codeigniterヘルパーの詳細を知るにはFunction https://ellislab.com/codeigniter/user-guide/general/helpers.html

2

Load lib。この機能は、検出する

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

使用がモバイルで

CodeIgniterでは、コントローラ内部 browser or agent detection in codeigniter

のサポートが組み込まれてい

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

次のサンプルコードを使用:それは完璧に動作

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