程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> thinkphp判斷訪客為手機端或PC端的方法,thinkphp訪客

thinkphp判斷訪客為手機端或PC端的方法,thinkphp訪客

編輯:關於PHP編程

thinkphp判斷訪客為手機端或PC端的方法,thinkphp訪客


本文實例講述了thinkphp判斷訪客為手機端或PC端的方法。分享給大家供大家參考。具體實現方法如下:

一、問題:

近日准備給自己的網站做一個小升級,讓用戶在手機二維碼掃描的時候顯示適合手機端來展示的模版【我用的是ThinkPHP3.0】,代碼是參考別人的

二、實現方法:

這裡先說下大概的一個思路 簡單兩步:

統版本號和浏覽器以及它的版本號(貼出的代碼中數組中只有移動端信息,因此後面只需判斷是否為數組中的某一個值即可).

根據數組中的值來判斷訪客是否為手機、pad、之類的移動端,如果是的話就規定項目路徑、名稱為你的手機端模版如復制代碼 代碼如下:define('APP_NAME','mobi');

define('APP_PATH','./mobi/');

下面代碼的話我盡量加上注釋:
復制代碼 代碼如下://判斷是否屬手機 
//代碼看上去很多,其實就是數組裡面顯得多而亂,不要被表面現象所嚇倒哦! 
 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)) { //stristr 查找訪客端信息是否在上述數組中,不存在即為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框架程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved