判断用户是否手机访问

随着移动设备的普及,网站也会迎来越来越多 移动设备 的访问。用适应PC的页面,很多时候对手机用户不友好,那么有些时候,我们需要判断用户是否用手机访问,如果是手机的话,就跳转到指定的手机友好页面。这里就介绍一下,如何判断用户是否用手机访问。
  1. <?php  
  2.     /* 
  3.     *@Descrition:判断用户是否手机访问 
  4.     * 
  5.     * 
  6.     */  
  7.     $agent = check_wap();  
  8.     if($agent){  
  9.         header('Location:http://www.goole.com');//重定向  
  10.         exit;  
  11.     }  
  12.   
  13.     //check if wap  
  14.     function check_wap(){  
  15.         //先检查是否为wap代理,准确度高  
  16.         if(stristr($_SERVER['HTTP_VIA'], 'wap')){  
  17.             return true;  
  18.         }  
  19.         //检查浏览器是否接受WML  
  20.         elseif(strpos(strtoupper($_SERVER['HTTP_ACCEPT']), 'VND.WAP.WML') > 0){  
  21.             return true;  
  22.         }  
  23.         //检查USER_AGENT  
  24.         elseif(preg_match('/(blackberry|configuration\/cldc|hp| hp-|htc_|htc-|googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipad|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sorce|xda |xda_)/i'$_SERVER['HTTP_USER_AGENT'])){  
  25.             return true;  
  26.         }else{  
  27.             return false;  
  28.         }  
  29.     }  
  30.   
  31.     //这个函数是从php框架剥离的判断是否为手机移动终端的函数  
  32.     function is_mobile_request(){  
  33.         $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : '';  
  34.         $mobile_browser = '0';  
  35.         if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i'strtolower($_SERVER['HTTP_USER_AGENT']))){  
  36.             $mobile_browser++;  
  37.         }  
  38.         if(isset($_SERVER['HTTP_X_WAP_FROFILE'])){  
  39.             $mobile_browser++;  
  40.         }  
  41.         if(isset($_SERVER['HTTP_PROFILE'])){  
  42.             $mobile_browser++;  
  43.         }  
  44.         $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));  
  45.         $mobile_agents = $mobile_agents = array(    
  46.             'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',    
  47.             'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',    
  48.             'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',    
  49.             'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',    
  50.             'newt','noki','oper','palm','pana','pant','phil','play','port','prox',    
  51.             'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',    
  52.             'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',    
  53.             'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',    
  54.             'wapr','webc','winw','winw','xda','xda-'   
  55.         );  
  56.         if(in_array($mobile_ua$mobile_agents)){  
  57.             $mobile_browser++;  
  58.         }  
  59.         if(strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false){  
  60.             $mobile_browser++;  
  61.         }  
  62.         //Pre-final check to reset everything if the user is on windows  
  63.         if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') !== false){  
  64.             $mobile_browser++;  
  65.         }  
  66.         if($mobile_browser > 0){  
  67.             return true;  
  68.         }else{  
  69.             return false;  
  70.         }  
  71.     }  
  72. ?>  

代码没有经过自己的测试。如果有错误,请指出!!!

代码来源地址:http://www.nowamagic.net/librarys/veda/detail/2499
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值