代碼中的函數 is_mobile來源於網上,寫得挺好的。入口文件通過判斷終端類型,定義APP_PATH和APP_NAME,從而執行不同的分組。我要實現的效果是PC和Mobile均用頂級域名訪問。當然了,如果你想在手機裡實現類似於wap.xxx.com這樣的訪問,本方法是做不到的,這是thinkphp多域名部署的問題,具體參見tp手冊。
用thinkphp框架快速開發一個小型站點還是很方便的,正如本站。菜根網還做了一個手機端的網站,手機浏覽器輸入本站域名www.icaigen.com查看效果。
下面僅僅講述在thinkphp的前端引導頁面index.php文件中實現平台判斷,從而加載不同的分組(或項目)。
//判斷是否屬手機 function is_mobile(){ $user_agent = $_SERVER['HTTP_USER_AGENT']; $mobile_agents = Array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte"); $is_mobile = false; foreach ($mobile_agents as $device) { if (stristr($user_agent, $device)) { $is_mobile = true; break; } } return $is_mobile; } define('THINK_PATH','./CORE/'); if(is_mobile()){ //跳轉至wap分組 define('APP_NAME','Wap'); define('APP_PATH','./Wap/'); }else{ define('APP_NAME','Home'); define('APP_PATH','./Home/'); } define('APP_DEBUG', false); require THINK_PATH.'core.php';代碼中的函數 is_mobile來源於網上,寫得挺好的。入口文件通過判斷終端類型,定義APP_PATH和APP_NAME,從而執行不同的分組。