首页 > 开发 > PHP > 正文

php判断用户是否手机访问代码

2024-05-04 23:36:01
字体:大 中 小
来源:转载
供稿:网友

之前有一个案例用到判断用户是手机访问还是电脑访问,然后给用户展示相应的页面。最初是用js去做,但是准确度不高,最终采用了PHP来做,经测试基本上都能判断准确。

随着移动设备的普及,网站也会迎来越来越多移动设备的访问。用适应PC的页面,很多时候对手机用户不友好,那么有些时候,我们需要判断用户是否用手机访问,如果是手机的话,就跳转到指定的手机友好页面。这里就介绍一下,如何判断用户是否用手机访问。

自定义的函数如下:

 

 
  1. $agent = check_wap(); 
  2. if( $agent ) 
  3. { 
  4. header('Location: http://www.vevb.com'); 
  5. exit; 
  6. } 
  7. // check if wap 
  8. function check_wap(){ 
  9. // 先检查是否为wap代理,准确度高 
  10. if(stristr($_SERVER['HTTP_VIA'],"wap")){ 
  11. return true; 
  12. } 
  13. // 检查浏览器是否接受 WML. 
  14. elseif(strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"VND.WAP.WML") > 0){ 
  15. return true; 
  16. } 
  17. //检查USER_AGENT 
  18. 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'])){ 
  19. return true;  
  20. } 
  21. else{ 
  22. return false;  
  23. } 
  24. } 

再来一个从PHP框架剥离的判断是否为手机移动终端的函数:

 

 
  1. function is_mobile_request()  
  2. {  
  3. $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : '';  
  4. $mobile_browser = '0';  
  5. if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', strtolower($_SERVER['HTTP_USER_AGENT'])))  
  6. $mobile_browser++;  
  7. if((isset($_SERVER['HTTP_ACCEPT'])) and (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') !== false))  
  8. $mobile_browser++;  
  9. if(isset($_SERVER['HTTP_X_WAP_PROFILE']))  
  10. $mobile_browser++;  
  11. if(isset($_SERVER['HTTP_PROFILE']))  
  12. $mobile_browser++;  
  13. $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));  
  14. $mobile_agents = array(  
  15. 'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',  
  16. 'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',  
  17. 'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',  
  18. 'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',  
  19. 'newt','noki','oper','palm','pana','pant','phil','play','port','prox',  
  20. 'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',  
  21. 'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',  
  22. 'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',  
  23. 'wapr','webc','winw','winw','xda','xda-' 
  24. );  
  25. if(in_array($mobile_ua, $mobile_agents))  
  26. $mobile_browser++;  
  27. if(strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false)  
  28. $mobile_browser++;  
  29. // Pre-final check to reset everything if the user is on Windows  
  30. if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') !== false)  
  31. $mobile_browser=0;  
  32. // But WP7 is also Windows, with a slightly different characteristic  
  33. if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows phone') !== false)  
  34. $mobile_browser++;  
  35. if($mobile_browser>0)  
  36. return true;  
  37. else 
  38. return false;  
  39. } 

再给大家分享一个个人使用的

 

 
  1. function isPhone() { 
  2. // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 
  3. if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) { 
  4. return true; 
  5. } 
  6. //如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息 
  7. if (isset($_SERVER['HTTP_VIA'])) { 
  8. //找不到为flase,否则为true 
  9. if(stristr($_SERVER['HTTP_VIA'], "wap")) 
  10. { 
  11. return true; 
  12. } 
  13. } 
  14. //脑残法,判断手机发送的客户端标志,兼容性有待提高 
  15. if (isset($_SERVER['HTTP_USER_AGENT'])) { 
  16. $clientkeywords = array ( 
  17. 'nokia', 
  18. 'sony', 
  19. 'ericsson', 
  20. 'mot', 
  21. 'samsung', 
  22. 'htc', 
  23. 'sgh', 
  24. 'lg', 
  25. 'sharp', 
  26. 'sie-', 
  27. 'philips', 
  28. 'panasonic', 
  29. 'alcatel', 
  30. 'lenovo', 
  31. 'iphone', 
  32. 'ipod', 
  33. 'blackberry', 
  34. 'meizu', 
  35. 'android', 
  36. 'netfront', 
  37. 'symbian', 
  38. 'ucweb', 
  39. 'windowsce', 
  40. 'palm', 
  41. 'operamini', 
  42. 'operamobi', 
  43. 'openwave', 
  44. 'nexusone', 
  45. 'cldc', 
  46. 'midp', 
  47. 'wap', 
  48. 'mobile', 
  49. 'phone', 
  50. ); 
  51. // 从HTTP_USER_AGENT中查找手机浏览器的关键字 
  52. if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) { 
  53. return true; 
  54. } 
  55. } 
  56. //协议法,因为有可能不准确,放到最后判断 
  57. if (isset($_SERVER['HTTP_ACCEPT'])) { 
  58. // 如果只支持wml并且不支持html那一定是移动设备 
  59. // 如果支持wml和html但是wml在html之前则是移动设备 
  60. if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) { 
  61. return true; 
  62. } 
  63. } 
  64. return false; 
  65. } 

以上所述就是本文的全部内容了,希望大家能够喜欢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表