文章詳細內(nèi)容 thinkphp如何來判斷訪客為手機端或是PC端 發(fā)布日期:2014年06月12日 來源: PHP1.CN 點擊:138528 摘要:近日準備給自己的網(wǎng)站做一個小升級,讓用戶在手機二維碼掃描的時候顯示適合手機端來展示的模版【我用的是ThinkPHP3.0】,代碼是參考別人的,我先說下大概的一個思路簡單兩步:統(tǒng)版本號.... 轉載請注明來源: thinkphp如何來判斷訪客為手機端或是PC端
近日準備給自己的網(wǎng)站做一個小升級,讓用戶在手機二維碼掃描的時候顯示適合手機端來展示的模版【我用的是ThinkPHP3.0】,代碼是參考別人的
我先說下大概的一個思路 簡單兩步:
統(tǒng)版本號和瀏覽器以及它的版本號(貼出的代碼中數(shù)組中只有移動端信息,因此后面只需判斷是否為數(shù)組中的某一個值即可).
根據(jù)數(shù)組中的值來判斷訪客是否為手機,、pad,、之類的移動端,如果是的話就規(guī)定項目路徑,、名稱為你的手機端模版如 define('APP_NAME','mobi');
define('APP_PATH','./mobi/');
下面代碼的話我盡量加上注釋:
- //判斷是否屬手機
- //代碼看上去很多,,其實就是數(shù)組里面顯得多而亂,不要被表面現(xiàn)象所嚇倒哦,!
- 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) {//這里把值遍歷一遍,,用于查找是否有上述字符串出現(xiàn)過
- if (stristr($user_agent, $device)) { //stristr 查找訪客端信息是否在上述數(shù)組中,不存在即為PC端,。
- $is_mobile = true;
- break;
- }
- }
- return $is_mobile;
- }
- define('THINK_PATH','./CORE/');
- if(is_mobile()){ //跳轉至wap分組
- echo '您是手機端訪問的,,已跳轉到手機端';
- define('APP_NAME','mobi');
- define('APP_PATH','./mobi/');
- }else{
- echo '你是PC端訪問的';
- define('APP_NAME','Home');
- define('APP_PATH','./Home/');
- }
- define('APP_DEBUG', false);
- require THINK_PATH.'core.php';
轉載請注明來源: thinkphp如何來判斷訪客為手機端或是PC端 http://www./Content/thinkphp_RuHeLaiPanDuanFangKeWeiShouJiDuanHuoShi_PC_Duan.html關注 第一PHP社區(qū) 微信公眾號,獲取更多最新PHP技術分享
|