php 判斷訪問者是否手機用戶端執行個體

來源:互聯網
上載者:User

方法一:判斷HTTP_USER_AGENT

 代碼如下 複製代碼

$agent = strtolower($_SERVER['HTTP_USER_AGENT']); 
if(strpos($agent,"netfront") || strpos($agent,"iphone") || strpos($agent,"midp-2.0") || strpos($agent,"opera mini") || strpos($agent,"ucweb") || strpos($agent,"android") || strpos($agent,"windows ce") || strpos($agent,"symbianos")) {
    Header("HTTP/1.1 301 Moved Permanently");
    header("Location:####");  die;
}


方法二:判斷HTTP_ACCEPT

 代碼如下 複製代碼
if (isset($_SERVER['HTTP_ACCEPT']) && (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')) )) {//手機訪問 
    Header("HTTP/1.1 301 Moved Permanently");
    header("Location:####"); die;
}

以上兩個方法都有局限性,

下面將此兩種方法整合起來判斷

 代碼如下 複製代碼

function isMobile() {
    if(isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
        return true;
    }
    if(isset ($_SERVER['HTTP_VIA'])) {
        //找不到為flase,否則為true
        return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
    }
    if(isset($_SERVER['HTTP_USER_AGENT'])) {
        //此數組有待完善
        $clientkeywords = array (
        'nokia',
        'sony',
        'ericsson',
        'mot',
        'samsung',
        'htc',
        'sgh',
        'lg',
        'sharp',
        'sie-',
        'philips',
        'panasonic',
        'alcatel',
        'lenovo',
        'iphone',
        'ipod',
        'blackberry',
        'meizu',
        'android',
        'netfront',
        'symbian',
        'ucweb',
        'windowsce',
        'palm',
        'operamini',
        'operamobi',
        'openwave',
        'nexusone',
        'cldc',
        'midp',
        'wap',
        'mobile'
        );
        // 從HTTP_USER_AGENT中尋找手機瀏覽器的關鍵字
        if(preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
            return true;
        }
 
    }
 
    //協議法,因為有可能不準確,放到最後判斷
    if (isset ($_SERVER['HTTP_ACCEPT'])) {
        // 如果只支援wml並且不支援html那一定是行動裝置
        // 如果支援wml和html但是wml在html之前則是行動裝置
        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')))) {
            return true;
        }
    }
     
return false;
}

上面的方法也存在一些小問題,這裡我根據自己的經驗來告訴大我們可以使用螢幕寬度來實現再加機器類型了,因為有時HTTP_USER_AGENT資訊在我們上面並未定義過了,不過上面實現幾乎相容了主流手機了。

我們還可以使用js

<html>

 <body>

  <script type="text/javascript">
   function browserRedirect() {
    var sUserAgent = navigator.userAgent.toLowerCase();
    var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
    var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
    var bIsMidp = sUserAgent.match(/midp/i) == "midp";
    var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
    var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
    var bIsAndroid = sUserAgent.match(/android/i) == "android";
    var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
    var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";

    if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
     window.location.href = 'http://url/mobile.html';
    } else {
     window.location = 'http://url/pc.html';
    }

   }

   browserRedirect();
  </script>

 </body>
</html> 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.