判断用户是否手机访问

来源:互联网 发布:linux cut 编辑:程序博客网 时间:2024/04/28 01:57
随着移动设备的普及,网站也会迎来越来越多移动设备的访问。用适应PC的页面,很多时候对手机用户不友好,那么有些时候,我们需要判断用户是否用手机访问,如果是手机的话,就跳转到指定的手机友好页面。这里就介绍一下,如何判断用户是否用手机访问。
[php] view plaincopy在CODE上查看代码片派生到我的代码片
  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
原创粉丝点击