判斷用戶端是電腦還是手機

來源:互聯網
上載者:User

標籤:android   cWeb   http   io   ar   os   sp   on   art   

  用php判斷用戶端是手機還是電腦?其實很簡單,通過瀏覽器訪問網站時,瀏覽器會向伺服器發送UA即(使用者代理程式)。不同瀏覽器或者同一瀏覽器的不同版本、手機瀏覽器、電腦瀏覽器的UA都是不一樣的。我們只要寫代程式擷取這段UA,然後判斷UA是什麼,然後就可以區分出用戶端是手機還是電腦了。

  function is_mobile(){

  //Regex,批配不同手機瀏覽器UA關鍵詞。

  $regex_match=”/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|”;

  http://www.douban.com/note/452245423/

  http://www.douban.com/note/452246041/

  $regex_match.=”htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|”;

  $regex_match.=”blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|”;

  $regex_match.=”symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|”;

  $regex_match.=”jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320×320|240×320|176×220″;

  $regex_match.=”)/i”;

  return isset($_SERVER[‘HTTP_X_WAP_PROFILE‘]) or isset($_SERVER[‘HTTP_PROFILE‘]) or preg_match($regex_match, strtolower($_SERVER[‘HTTP_USER_AGENT‘]));

  //如果UA中存在上面的關鍵詞則返回真。

  }

  調用

  if($this->is_mobile()) {

  //如果為真,則進行WAP頁面。

  header( “HTTP/1.1 301 Moved Permanently”);

  header(“Location: http://wap”);

  }else {

  //如果為假,則進行WEB頁面。

  header( “HTTP/1.1 301 Moved Permanently”);

  header(“Location: http://pc”);

  }

判斷用戶端是電腦還是手機

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.