程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php如何判斷用戶通過手機wap訪問還是電腦直接訪問

php如何判斷用戶通過手機wap訪問還是電腦直接訪問

編輯:關於PHP編程

現在我們智能手機用戶很多了,訪問網站也有很多朋友使用手機直接訪問,這樣存在問題了如果是pc版本手機訪問肯定不好看還費人家流量了,所以很多公司做了wap站了,但是要如何判斷用戶是手機還是pc訪問網站呢,下面我整理了一些方法。

最近做一個手機查詢系統,自然就牽扯到了此問題,那我就根據對wap的認識淺談下通過php判斷用戶訪問方式是通過wap訪問還是電腦直接訪問。
首先說最根本的解決方法:
手機訪問時,會附帶發送user-agent信息,這個信息裡面會有手機號碼信息,那麼如果能取得手機號碼,則可以肯定是通過手機wap訪問的。但是目前中國移動已經屏蔽了user-agent信息,所以獲取不到手機號碼。有關系的朋友可以聯系移動公司,把wap網站服務器的ip提交給中國移動,加入白名單後即可取得ua信息。目前中國聯通可以直接取到手機號,對聯通用戶此方案可完美實施。
接下來說我的解決方案:
手機訪問,原理是手機通過移動公司的代理服務器進行的訪問。那麼我們就可以理解是一台普通電腦使用了代理服務器。當手機通過代理服務器訪問的時候,http頭信息會毫無疑問的包含一個信息:via。這個信息提供了有價值的判斷信息。
例如河南移動取得的via信息是:
http/1.1 hazz-b-gw001-wap(infox-wisg, huawei technologies)
河南聯通的via信息是:
zxwap gateway,zte technologies
其他各省的http頭信息和這個大同小異,判斷是否手機訪問的方案就出來了:獲取http的via信息字符串看是否包含wap字符,如果有則是通過手機訪問。這樣做的結果是沒有人能偽造手機訪問,判斷絕對准確。自然,這樣對於網上流行的手機wap模擬器也做了屏蔽----從根本上屏蔽。
操作代碼也很簡單:

 代碼如下 復制代碼 // check if wap by xhat
function check_wap() {
return stristr($_SERVER['HTTP_VIA'],"wap") ? true : false;
}
// check over

從我查閱的資料來看,目前此方法應該是迄今互聯網上准確率最高、最簡單的判斷手機訪問方法。

iphone智能手機

為discuz做的iphone版本基本完工,為了方便訪問,直接在首頁對iphone的訪問進行了判斷,然後直接跳轉

使用以上代碼判斷,經測試效果很好。

自己用的一個判斷類,比較全面了

 代碼如下 復制代碼

/*
 判斷訪問用戶是否為手機bKjia.c0m
 //判斷是否屬手機
 */
 function is_mobile() {
  $user_agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  //echo $user_agent;
  $mobile_agents = Array("ipad","wap","android","iphone","sec","sam","ericsson","240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","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","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","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte","ben","hai","phili");
  $is_mobile = false;
  foreach ($mobile_agents as $device) {
   if (stristr($user_agent, $device)) {
    if( 'ipad' == $device )
    {
     return $is_mobile;
    }
    $is_mobile = true;
    break;
   }
  }
  return $is_mobile;
 }

 

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