CoreThink開發(十一)首頁控制器判斷移動設備還是PC並做相應處理
阿新 • • 發佈:2017-12-22
iss htm meizu http nec window sam assign clas
在home模塊Index控制器添加判斷代碼
application\Home\Controller\IndexController.class.php
<?php // +---------------------------------------------------------------------- // | OpenCMF [ Simple Efficient Excellent ] // +---------------------------------------------------------------------- // | Copyright (c) 2014 http://www.opencmf.cn All rights reserved. // +---------------------------------------------------------------------- // | Author: jry <[email protected]> // +---------------------------------------------------------------------- namespace Home\Controller; use Think\Controller; /** * 前臺默認控制器 * @author jry <[email protected]> */ class IndexController extends HomeController { /** * 默認方法 * @author jry <[email protected]> */ public function index() { Cookie(‘__forward__‘, C(‘HOME_PAGE‘)); if ($this->isMobile()) { $this->redirect(‘/cms/index/lists/cid/1.html‘);//使手機重定向到Mobile模塊 } if (C(‘INDEX_URL‘)) { if (stristr(C(‘INDEX_URL‘), ‘http://‘) || stristr(C(‘INDEX_URL‘), ‘https://‘)) { redirect(C(‘INDEX_URL‘)); } else { $this->redirect(C(‘INDEX_URL‘)); } } $this->assign(‘meta_title‘, "首頁"); $this->display(); } function isMobile(){ // 如果有HTTP_X_WAP_PROFILE則一定是移動設備 if (isset ($_SERVER[‘HTTP_X_WAP_PROFILE‘])) return true; //此條摘自TPM智能切換模板引擎,適合TPM開發 if(isset ($_SERVER[‘HTTP_CLIENT‘]) &&‘PhoneClient‘==$_SERVER[‘HTTP_CLIENT‘]) return true; //如果via信息含有wap則一定是移動設備,部分服務商會屏蔽該信息 if (isset ($_SERVER[‘HTTP_VIA‘])) //找不到為flase,否則為true return stristr($_SERVER[‘HTTP_VIA‘], ‘wap‘) ? true : false; //判斷手機發送的客戶端標誌,兼容性有待提高 if (isset ($_SERVER[‘HTTP_USER_AGENT‘])) { $clientkeywords = array( ‘nokia‘,‘sony‘,‘ericsson‘,‘mot‘,‘samsung‘,‘htc‘,‘sgh‘,‘lg‘,‘sharp‘,‘sie-‘,‘philips‘,‘panasonic‘,‘alcatel‘,‘lenovo‘,‘iphone‘,‘ipod‘,‘blackberry‘,‘meizu‘,‘android‘,‘netfront‘,‘symbian‘,‘ucweb‘,‘windowsce‘,‘palm‘,‘operamini‘,‘operamobi‘,‘openwave‘,‘nexusone‘,‘cldc‘,‘midp‘,‘wap‘,‘mobile‘ ); //從HTTP_USER_AGENT中查找手機瀏覽器的關鍵字 if (preg_match("/(" . implode(‘|‘, $clientkeywords) . ")/i", strtolower($_SERVER[‘HTTP_USER_AGENT‘]))) { return true; } } //協議法,因為有可能不準確,放到最後判斷 if (isset ($_SERVER[‘HTTP_ACCEPT‘])) { // 如果只支持wml並且不支持html那一定是移動設備 // 如果支持wml和html但是wml在html之前則是移動設備 if ((strpos($_SERVER[‘HTTP_ACCEPT‘], ‘vnd.wap.wml‘) !== false) && (strpos($_SERVER[‘HTTP_ACCEPT‘], ‘text/html‘) === false || (strpos($_SERVER[‘HTTP_ACCEPT‘], ‘vnd.wap.wml‘) < strpos($_SERVER[‘HTTP_ACCEPT‘], ‘text/html‘)))) { return true; } } return false; } }
isMobile判斷方法來自網絡.
CoreThink開發(十一)首頁控制器判斷移動設備還是PC並做相應處理