首先介紹APP_STATUS內置常量,TP入口文件增長APP_STATUS 參數, 自動加載不一樣的項目配置文件,經過配置文件指向不一樣的模塊php
手機端訪問時調用Wap手機模塊,實如今手機端訪問時展現出手機網站,無需跳轉域名
首先咱們在./Application/Common/Conf/ 目錄下創建兩個公共配置文件:config.php 和Mobie.phphtml
config.php文件中 java
<?php
return array( 'DEFAULT_MODULE'=>'Index', 'DEFAULT_CONTROLLER'=>'Index', 'DEFAULT_ACTION'=>'index', );
Mobie.php文件:android
<?php
return array( 'DEFAULT_MODULE'=>'Mobie', 'DEFAULT_CONTROLLER'=>'Index', 'DEFAULT_ACTION'=>'index', );
關鍵在單入口index文件中添加手機端判斷方法(方法建議放到最下面),在文件中的21行後面也就是「定義應用目錄」結束後,加入代碼:web
if(is_mobile_request()){ define('APP_STATUS','config_wap'); }else{ define('APP_STATUS','Index'); }
這樣,就用關鍵的APP_STATUS來調用不一樣的模塊,is_mobile_request()這樣寫:thinkphp
function is_mobile_request(){ $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : ''; $mobile_browser = '0'; if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) $mobile_browser++; if((isset($_SERVER['HTTP_ACCEPT'])) and (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') !== false)) $mobile_browser++; if(isset($_SERVER['HTTP_X_WAP_PROFILE'])) $mobile_browser++; if(isset($_SERVER['HTTP_PROFILE'])) $mobile_browser++; $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4)); $mobile_agents = array( 'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac', 'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno', 'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-', 'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-', 'newt','noki','oper','palm','pana','pant','phil','play','port','prox', 'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar', 'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-', 'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp', 'wapr','webc','winw','winw','xda','xda-' ); if(in_array($mobile_ua, $mobile_agents)) $mobile_browser++; if(strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false) $mobile_browser++; // Pre-final check to reset everything if the user is on Windows if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') !== false) $mobile_browser=0; // But WP7 is also Windows, with a slightly different characteristic if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows phone') !== false) $mobile_browser++; if($mobile_browser>0) return true; else return false; }
參考TP文檔:http://document.thinkphp.cn/manual_3_2.html#load_configwindows
我還查到用JS判斷的,沒去親自驗證,代碼以下:api
(function(){ var res = GetRequest(); var par = res['index']; if(par!='gfan'){ var ua=navigator.userAgent.toLowerCase(); var contains=function (a, b){ if(a.indexOf(b)!=-1){return true;} }; var toMobileVertion = function(){ window.location.href = '手機站地址' } if(contains(ua,"ipad")||(contains(ua,"rv:1.2.3.4"))||(contains(ua,"0.0.0.0"))||(contains(ua,"8.0.552.237"))){return false} if((contains(ua,"android") && contains(ua,"mobile"))||(contains(ua,"android") && contains(ua,"mozilla")) ||(contains(ua,"android") && contains(ua,"opera")) ||contains(ua,"ucweb7")||contains(ua,"iphone")){toMobileVertion();} } })(); function GetRequest() { var url = location.search; //獲取url中"?"符後的字串 var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for(var i = 0; i < strs.length; i ++) { theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]); } } return theRequest; }