PHP判斷訪問者是PC端還是移動端

來源:互聯網
上載者:User

標籤:gen   comment   用戶端   cat   phone   fun   cep   imp   dma   

  1. function isMobile()
  2.     // 如果有HTTP_X_WAP_PROFILE則一定是行動裝置
  3.     if (isset ($_SERVER[‘HTTP_X_WAP_PROFILE‘]))
  4.     {
  5.         return true;
  6.     } 
  7.     // 如果via資訊含有wap則一定是行動裝置,部分服務商會屏蔽該資訊
  8.     if (isset ($_SERVER[‘HTTP_VIA‘]))
  9.     { 
  10.         // 找不到為flase,否則為true
  11.         return stristr($_SERVER[‘HTTP_VIA‘], "wap") ? true : false;
  12.     } 
  13.     // 腦殘法,判斷手機發送的用戶端標誌,相容性有待提高
  14.     if (isset ($_SERVER[‘HTTP_USER_AGENT‘]))
  15.     {
  16.         $clientkeywords = array (‘nokia‘,
  17.             ‘sony‘,
  18.             ‘ericsson‘,
  19.             ‘mot‘,
  20.             ‘samsung‘,
  21.             ‘htc‘,
  22.             ‘sgh‘,
  23.             ‘lg‘,
  24.             ‘sharp‘,
  25.             ‘sie-‘,
  26.             ‘philips‘,
  27.             ‘panasonic‘,
  28.             ‘alcatel‘,
  29.             ‘lenovo‘,
  30.             ‘iphone‘,
  31.             ‘ipod‘,
  32.             ‘blackberry‘,
  33.             ‘meizu‘,
  34.             ‘android‘,
  35.             ‘netfront‘,
  36.             ‘symbian‘,
  37.             ‘ucweb‘,
  38.             ‘windowsce‘,
  39.             ‘palm‘,
  40.             ‘operamini‘,
  41.             ‘operamobi‘,
  42.             ‘openwave‘,
  43.             ‘nexusone‘,
  44.             ‘cldc‘,
  45.             ‘midp‘,
  46.             ‘wap‘,
  47.             ‘mobile‘
  48.             ); 
  49.         // 從HTTP_USER_AGENT中尋找手機瀏覽器的關鍵字
  50.         if (preg_match("/(" . implode(‘|‘, $clientkeywords) . ")/i", strtolower($_SERVER[‘HTTP_USER_AGENT‘])))
  51.         {
  52.             return true;
  53.         } 
  54.     } 
  55.     // 協議法,因為有可能不準確,放到最後判斷
  56.     if (isset ($_SERVER[‘HTTP_ACCEPT‘]))
  57.     { 
  58.         // 如果只支援wml並且不支援html那一定是行動裝置
  59.         // 如果支援wml和html但是wml在html之前則是行動裝置
  60.         if ((strpos($_SERVER[‘HTTP_ACCEPT‘], ‘vnd.wap.wml‘) !== false) && (strpos($_SERVER[‘HTTP_ACCEPT‘], ‘text/html‘) === false || (strpos($_SERVER[‘HTTP_ACCEPT‘], ‘vnd.wap.wml‘) < strpos($_SERVER[‘HTTP_ACCEPT‘], ‘text/html‘))))
  61.         {
  62.             return true;
  63.         } 
  64.     } 
  65.     return false;
 if($this->isMobile()){//跳轉移動端頁面}else{//跳轉PC端頁面}

 ======================

 

首先推薦一個php輕量級識別類,Mobile-Detect 專門識別是手機端還是pc端訪問網站,這樣就可以根據訪問的終端類型指向手機瀏覽器適配的網站還是pc瀏覽器的網站。

Mobile-Detect官網連結如下MobileDetect   

樣本連結如下:Mobile-Detect Example

 

下面是我寫得簡單的跳轉適配PC端還是手機端的代碼:

 

[php] view plain copy 
  1. <?php  
  2.     require_once ‘Mobile_Detect.php‘;    //注意要引入Mobile_Detect.php  這個類在上文的串連中有下載連結  
  3.     $detect = new Mobile_Detect;  
  4.     if($detect->isMobile()){  
  5.          header(‘Location: http://127.0.0.1/MobileDetect/MobileDetect/mobile.html‘, true, 301);  
  6.          echo "mobile";  
  7.     }else{  
  8.          header(‘Location: http://127.0.0.1/MobileDetect/MobileDetect/pc.html‘, true, 301);  
  9.          echo "pc";  
  10.     }  
  11.   
  12.   
  13. ?>  

PHP判斷訪問者是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.