您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)thinkphp如何判斷訪客為手機(jī)端或PC端,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
一、問題:
近日準(zhǔn)備給自己的網(wǎng)站做一個(gè)小升級(jí),讓用戶在手機(jī)二維碼掃描的時(shí)候顯示適合手機(jī)端來展示的模版【我用的是ThinkPHP3.0】,代碼是參考別人的
二、實(shí)現(xiàn)方法:
這里先說下大概的一個(gè)思路 簡(jiǎn)單兩步:
統(tǒng)版本號(hào)和瀏覽器以及它的版本號(hào)(貼出的代碼中數(shù)組中只有移動(dòng)端信息,因此后面只需判斷是否為數(shù)組中的某一個(gè)值即可).
根據(jù)數(shù)組中的值來判斷訪客是否為手機(jī)、pad、之類的移動(dòng)端,如果是的話就規(guī)定項(xiàng)目路徑、名稱為你的手機(jī)端模版如
復(fù)制代碼 代碼如下:
define('APP_NAME','mobi');
define('APP_PATH','./mobi/');
下面代碼的話我盡量加上注釋:
//判斷是否屬手機(jī) //代碼看上去很多,其實(shí)就是數(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()){ //跳轉(zhuǎn)至wap分組 echo '您是手機(jī)端訪問的,已跳轉(zhuǎn)到手機(jī)端'; 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';
關(guān)于“thinkphp如何判斷訪客為手機(jī)端或PC端”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。