2019
10-14

使用PHP、js判断访问是否为手机端,判断访问用户是不是手机端客户端请求头信息

路由器交流群

使用PHP、js判断访问是否为手机端,判断访问用户是不是手机端

我们在做简单开发的时候,常常需要判断来访者是手机还是 PC。比如之前在写一个 WordPress 小功能的时候只是需要简单判断来访者是 Mac 电脑即执行:

$agent = strtolower($_SERVER['HTTP_USER_AGENT']);

$mac =  (strpos($agent, 'mac os x'));

if(!$mac){

   echo "不是 Mac 设备!";

} else {

   echo "我是执行代码"; //执行所需要运行的代码

}

下面是所有几个最简单的判断代码:

$agent = strtolower($_SERVER['HTTP_USER_AGENT']);

$is_mac = (strpos($agent, 'mac os x')) ? true : false;

$is_pc = (strpos($agent,'windows nt')) ? true:false;

$is_iphone = (strpos($agent,'iphone')) ? true:false;

$is_ipad = (strpos($agent,'ipad')) ? true:false;

$is_android = (strpos($agent,'android')) ? true:false;

if($is_mac){

   echo "这是 mac";

}

if($is_pc) {

echo "这是 PC";

}

if($is_iphone) {

echo "这是 iPhone";

}

if($is_ipad) {

echo "这是 iPad";

}

if($is_android) {

echo "这是 Android";

}

js判断是否为手机访问

例子一

<script type="text/javascript">

var ua = navigator.userAgent;

var ipad = ua.match(/(iPad).*OS\s([\d_]+)/),

isIphone =!ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/),

isAndroid = ua.match(/(Android)\s+([\d.]+)/),

isMobile = isIphone || isAndroid;

//判断

if(isMobile){

}else{

}

</script>

例子二

<script>

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='//www.jb51.net';

  }else{

 //window.location.href='http://m.jb51.net'; 

 }

}

$(function(){

 browserRedirect();

});

</script>

例子三

<script type="text/javascript">

(function(){

  var reWriteUrl = function(url){

    if(url){

      var Splits = url.split("/"),siteName=window.location.pathname;

      if(typeof siteName!=="undefined"){

        return "http://m.jb51.net"+siteName;

      }

    }

  };

  if(/Android|webOS|iPhone|iPad|Windows Phone|iPod|BlackBerry|SymbianOS|Nokia|Mobile/i.test(navigator.userAgent)){

    var url=window.location.href;

    var pathname=window.location.pathname;

    if(url.indexOf("?mobile")<0){

    try{

      window.location.href=reWriteUrl(url);

    }catch(e){}

    }

  };

})();

</script>

如果需要给搜索引擎看

注意,如果给搜索引擎看的话我们在文件头加入
<meta http-equiv="mobile-agent" content="format=xhtml; url=手机端网址" />
<meta http-equiv="mobile-agent" content="format=html5; url=手机端网址" />

首先收集了部分客户端请求头部信息如下

iPhone微信

User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_1_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12B440 MicroMessenger/6.2.4 NetType/WIFI Language/en

iPhone—safari

User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_1_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12B440 Safari/600.1.4

Android-Chrome

User-Agent:Mozilla/5.0 (Linux; Android 4.4.4; HTC D820u Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.89 Mobile Safari/537.36

PC-Chrome

User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36

PC-Firefox

User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0

PC-IE

User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko

您可能感兴趣的文章

免费支持本站

支付宝打赏支付宝打赏微信打赏微信打赏

本文》有 0 条评论

留下一个回复 (您的评论需要经过审核才能显示)