识别是手机访问还是电脑访问(PHP和JS)

来源:互联网 发布:连云港seo服务 编辑:程序博客网 时间:2024/06/03 18:36

PHP版:

<?phpfunction isMobile(){  <span style="white-space:pre"></span>//HTTP_USER_AGENT是用来获取用户相关信息,包括了用户浏览器,操作系统工程,是否安装了alex toolbar条等<span style="white-space:pre"></span>$useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; <span style="white-space:pre"></span>//$matchs是存储过滤结果的数组 <span style="white-space:pre"></span>$useragent_commentsblock=preg_match('|\(.*?\)|',$useragent,$matches)>0?$matches[0]:'';  <span style="white-space:pre"></span>  <span style="white-space:pre"></span>function CheckSubstrs($substrs,$text){  <span style="white-space:pre"></span>foreach($substrs as $substr)<span style="white-space:pre"></span>    //strpos:查找$substr在$text中第一次出现的位置  <span style="white-space:pre"></span>if(false!==strpos($text,$substr)){  <span style="white-space:pre"></span>return true;  <span style="white-space:pre"></span>}  <span style="white-space:pre"></span>return false;  <span style="white-space:pre"></span>}<span style="white-space:pre"></span>$mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAQ');<span style="white-space:pre"></span>$mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod');  <span style="white-space:pre"></span>  <span style="white-space:pre"></span>$found_mobile=CheckSubstrs($mobile_os_list,$useragent_commentsblock) ||  <span style="white-space:pre"></span>  CheckSubstrs($mobile_token_list,$useragent);  <span style="white-space:pre"></span>  <span style="white-space:pre"></span>if ($found_mobile){  <span style="white-space:pre"></span>return true;  <span style="white-space:pre"></span>}else{  <span style="white-space:pre"></span>return false;  <span style="white-space:pre"></span>}  }if (isMobile())<span style="white-space:pre"></span>echo '手机登录m.php100.com';else<span style="white-space:pre"></span>echo '电脑登录www.php100.com';?>


JS版本:

第一种:

function IsPC() {    var userAgentInfo = navigator.userAgent;    var Agents = ["Android", "iPhone",                "SymbianOS", "Windows Phone",                "iPad", "iPod"];    var flag = true;    for (var v = 0; v < Agents.length; v++) {        if (userAgentInfo.indexOf(Agents[v]) > 0) {            flag = false;            break;        }    }    return flag;}

第二种:

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=B页面;    }}browserRedirect();

第三种:

function uaredirect(f) { try {  if (document.getElementById("bdmark") != null) {   return  }  var b = false;  if (arguments[1]) {   var e = window.location.host;   var a = window.location.href;   if (isSubdomain(arguments[1], e) == 1) {    f = f + "/#m/" + a;    b = true   } else {    if (isSubdomain(arguments[1], e) == 2) {     f = f + "/#m/" + a;     b = true    } else {     f = a;     b = false    }   }  } else {   b = true  }  if (b) {   var c = window.location.hash;   if (!c.match("fromapp")) {    if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|SymbianOS)/i))) {     location.replace(f)    }   }  } } catch(d) {}}function isSubdomain(c, d) { this.getdomain = function(f) {  var e = f.indexOf("://");  if (e > 0) {   var h = f.substr(e + 3)  } else {   var h = f  }  var g = /^www\./;  if (g.test(h)) {   h = h.substr(4)  }  return h }; if (c == d) {  return 1 } else {  var c = this.getdomain(c);  var b = this.getdomain(d);  if (c == b) {   return 1  } else {   c = c.replace(".", "\\.");   var a = new RegExp("\\." + c + "$");   if (b.match(a)) {    return 2   } else {    return 0   }  } }};使用方法:<SCRIPT type=text/javascript>uaredirect("手机站","WEB站");</SCRIPT>



0 0
原创粉丝点击