码迷,mamicode.com
首页 > 移动开发 > 详细

php判断手机访问还是电脑访问示例分享

时间:2015-11-30 14:31:54      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

示例一(推荐):

<?php
header("Content-type:text/html;charset=utf-8");
function isMobile(){ 
 $useragent=isset($_SERVER[‘HTTP_USER_AGENT‘]) ? $_SERVER[‘HTTP_USER_AGENT‘] : ‘‘; 
 $useragent_commentsblock=preg_match(‘|\(.*?\)|‘,$useragent,$matches)>0?$matches[0]:‘‘;    
 function CheckSubstrs($substrs,$text){ 
  foreach($substrs as $substr) 
   if(false!==strpos($text,$substr)){ 
    return true; 
   } 
   return false; 
 }
 $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‘);
 $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‘); 

 $found_mobile=CheckSubstrs($mobile_os_list,$useragent_commentsblock) || 
     CheckSubstrs($mobile_token_list,$useragent); 

 if ($found_mobile){ 
  return true; 
 }else{ 
  return false; 
 } 
}
if (isMobile())
 echo ‘手机登录‘;
else
 echo ‘电脑登录‘;
?>

示例二:

<?php
header("Content-type:text/html;charset=utf-8");
$agent = check_wap();
if( $agent )
 echo ‘手机登录‘;
else
 echo ‘电脑登录‘;
// check if wap 
function check_wap(){
  // 先检查是否为wap代理,准确度高
  if(@stristr($_SERVER[‘HTTP_VIA‘],"wap")){
    return true;
  }
  // 检查浏览器是否接受 WML.
  elseif(strpos(strtoupper($_SERVER[‘HTTP_ACCEPT‘]),"VND.WAP.WML") > 0){
    return true;
  }
  //检查USER_AGENT
  elseif(preg_match(‘/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i‘, $_SERVER[‘HTTP_USER_AGENT‘])){
    return true;       
  }
  else{
    return false;  
  }
}
?>

参考地址:http://www.jb51.net/article/45951.htm

      http://www.jb51.net/article/42664.htm

php判断手机访问还是电脑访问示例分享

标签:

原文地址:http://www.cnblogs.com/harxingxing/p/5007054.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!