CodeIgniterで端末接続種別を判定する
CodeIgniterには、User_Agentを判別するライブラリ、ヘルパーが準備されています。
が、それは日本のモバイルには対応しておらず、日本ユーザー会で独自の日本のキャリアを意識したUser_Agent取得のファイルを日本語版パッケージに同梱して配布しています。
ユーザー会HPはこちら。日本CodeIgniterユーザ会
で、携帯かどうかを判定し、携帯の端末IDを取得するライブラリを作成しました。
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /** * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * * @category * @package Get Japanese Mobile ID Class * @author KUNIHARU Tsujioka <kunitsuji@gmail.com> * @copyright Copyright (c) 2008 KUNIHARU Tsujioka <kunitsuji@gmail.com> * @copyright Copyright (c) 2006-2008 Usagi Project (URL:http://usagi.mynets.jp) * @license New BSD License */ /** * 端末IDを取得する */ class Mobileid { private $_uaObj = NULL; private $_id = ''; private $_ua = NULL; private $_carrier = 'other'; private $_ismobile = FALSE; /** * constructor * * @access public */ public function __construct() { $CI =& get_instance(); //autoloadで読み込んでいない場合 //$CI->load->library('user_agent'); $this->_ua = $CI->agent->agent_string(); $this->_setID(); } public function isMobile() { return $this->_ismobile; } public function getAgent() { return $this->_ua; } public function getMobileId() { return $this->_id; } private function _setCarrier($carrier) { $this->_carrier = $carrier; } public function getCarrier() { return $this->_carrier; } /** * IDをセットする * * @access private * @return string 端末ID(取得できなかった場合は空文字列) */ private function _setID() { $id = ''; // DoCoMo if (!strncmp($this->_ua, 'DoCoMo', 6)) { //imodeIDがある場合 if (isset($_SERVER['HTTP_X_DCMGUID'])) { $id = $_SERVER['HTTP_X_DCMGUID']; } else { // mova if (substr($this->_ua, 7, 3) === '1.0') { // 『/』区切りで最後のものを取る $pieces = explode('/', $this->_ua); $ser = array_pop($pieces); if (!strncmp($ser, 'ser', 3)) { $id = $ser; } } // FOMA elseif (substr($this->_ua, 7, 3) === '2.0') { $icc = substr($this->_ua, -24, -1); if (!strncmp($icc, 'icc', 3)) { $id = $icc; } } } $this->_setCarrier("docomo"); $this->_ismobile = TRUE; } // Vodafone(PDC) elseif (!strncmp($this->_ua, 'J-PHONE', 7)) { $pieces = explode('/', $this->_ua); $piece_sn = explode(' ', $pieces[3]); $sn = array_shift($piece_sn); if (!strncmp($sn, 'SN', 2)) { $id = $sn; } $this->_setCarrier("softbank"); $this->_ismobile = TRUE; } // Vodafone(3G) //* Up.Browser を搭載しているものがある(auより先に評価) //* MOTは製造番号を取得できない elseif (!strncmp($this->_ua, 'Vodafone', 8)) { $pieces = explode('/', $this->_ua); $piece_sn = explode(' ', $pieces[4]); $sn = array_shift($piece_sn); if (!strncmp($sn, 'SN', 2)) { $id = $sn; } $this->_setCarrier("softbank"); $this->_ismobile = TRUE; } // SoftBank elseif (!strncmp($this->_ua, 'SoftBank', 8)) { $pieces = explode('/', $ua); $piece_sn = explode(' ', $pieces[4]); $sn = array_shift($piece_sn); if (!strncmp($sn, 'SN', 2)) { $id = $sn; } $this->_setCarrier("softbank"); $this->_ismobile = TRUE; } // au elseif (!strncmp($this->_ua, 'KDDI', 4) || !strncasecmp($this->_ua, 'up.browser', 10) ) { //サブスクラバID(au) if ($_SERVER['HTTP_X_UP_SUBNO']) { $id = $_SERVER['HTTP_X_UP_SUBNO']; } $this->_setCarrier("au"); $this->_ismobile = TRUE; } // emobile elseif (strpos($this->_ua, 'emobile') !== false) { //サブスクラバID(au) if ($_SERVER['HTTP_X_EM_UID']) { $id = $_SERVER['HTTP_X_EM_UID']; } $this->_setCarrier("emobile"); $this->_ismobile = TRUE; } $this->_id = $id; } } ?>
CIでサイト構築されている方は、どうぞ。
追記
// WILLCOM / DDIPOCKET elseif (strpos($this->_ua, 'WILLCOM') !== false || strpos($this->_ua, 'SHARP/WS') !== false || strpos($this->_$ua, 'DDIPOCKET') !== false) { $this->_setCarrier("willcom"); $this->_ismobile = TRUE; }