当前位置:首页 > PHP教程 > php函数 > 列表

PHP 判断移动设备的函数isMobile()

发布:smiling 来源: PHP粉丝网  添加日期:2018-09-18 09:15:55 浏览: 评论:0 

不废话上代码,使用方法就是:

  1. <?php  
  2.  
  3. if(isMobile()){} 
  4.  
  5. if(!isMobile()){} 
  6.  
  7.  ?> 
  8.      
  9.  
  10. function isMobile() { 
  11.     $user_agent = $_SERVER['HTTP_USER_AGENT']; 
  12.  
  13. $mobile_agents = Array("240x320""acer""acoon""acs-""abacho""ahong""airness""alcatel""amoi""android""anywhereyougo.com""applewebkit/525""applewebkit/532""asus""audio""au-mic""avantogo""becker""benq""bilbo""bird""blackberry""blazer""bleu""cdm-""compal""coolpad""danger""dbtel""dopod""elaine""eric""etouch""fly ""fly_""fly-""go.web""goodaccess""gradiente""grundig""haier""hedy""hitachi""htc""huawei""hutchison""inno""ipad""ipaq""ipod""jbrowser""kddi""kgt""kwc""lenovo""lg ""lg2""lg3""lg4""lg5""lg7""lg8""lg9""lg-""lge-""lge9""longcos""maemo""mercator""meridian""micromax""midp""mini""mitsu""mmm""mmp""mobi""mot-""moto""nec-""netfront""newgen""nexian""nf-browser""nintendo""nitro""nokia""nook""novarra""obigo""palm""panasonic""pantech""philips""phone""pg-""playstation""pocket""pt-""qc-""qtek""rover""sagem""sama""samu""sanyo""samsung""sch-""scooter""sec-""sendo""sgh-""sharp""siemens""sie-""softbank""sony""spice""sprint""spv""symbian""tablet""talkabout""tcl-""teleca""telit""tianyu""tim-""toshiba""tsm""up.browser""utec""utstar""verykool""virgin""vk-""voda""voxtel""vx""wap""wellco""wig browser""wii""windows ce""wireless""xda""xde""zte"); 
  14.     $is_mobile = false; 
  15.  
  16. foreach ($mobile_agents as $device) { 
  17.  
  18.   if (stristr($user_agent$device)) { 
  19.  
  20.     $is_mobile = true; 
  21.  
  22.     break
  23.  
  24.   } 
  25.  
  26.  
  27. return $is_mobile
  28.  

网友补充了一个:

  1. <?php 
  2.  
  3. function isMobile() 
  4.  
  5. {  
  6.  
  7.     // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 
  8.  
  9.     if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) 
  10.  
  11.     { 
  12.  
  13.         return true; 
  14.  
  15.     }  
  16.  
  17.     // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息 
  18.  
  19.     if (isset ($_SERVER['HTTP_VIA'])) 
  20.  
  21.     {  
  22.  
  23.         // 找不到为flase,否则为true 
  24.  
  25.         return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false; 
  26.  
  27.     }  
  28.  
  29.     // 脑残法,判断手机发送的客户端标志,兼容性有待提高 
  30.  
  31.     if (isset ($_SERVER['HTTP_USER_AGENT'])) 
  32.  
  33.     { 
  34.  
  35.         $clientkeywords = array ('nokia'
  36.  
  37.             'sony'
  38.  
  39.             'ericsson'
  40.  
  41.             'mot'
  42.  
  43.             'samsung'
  44.  
  45.             'htc'
  46.  
  47.             'sgh'
  48.  
  49.             'lg'
  50.  
  51.             'sharp'
  52.  
  53.             'sie-'
  54.  
  55.             'philips'
  56.  
  57.             'panasonic'
  58.  
  59.             'alcatel'
  60.  
  61.             'lenovo'
  62.  
  63.             'iphone'
  64.  
  65.             'ipod'
  66.  
  67.             'blackberry'
  68.  
  69.             'meizu'
  70.  
  71.             'android'
  72.  
  73.             'netfront'
  74.  
  75.             'symbian'
  76.  
  77.             'ucweb'
  78.  
  79.             'windowsce'
  80.  
  81.             'palm'
  82.  
  83.             'operamini'
  84.  
  85.             'operamobi'
  86.  
  87.             'openwave'
  88.  
  89.             'nexusone'
  90.  
  91.             'cldc'
  92.  
  93.             'midp'
  94.  
  95.             'wap'
  96.  
  97.             'mobile' 
  98.  
  99.             );  
  100.  
  101.         // 从HTTP_USER_AGENT中查找手机浏览器的关键字 
  102.  
  103.         if (preg_match("/(" . implode('|'$clientkeywords) . ")/i"strtolower($_SERVER['HTTP_USER_AGENT']))) 
  104.  
  105.         { 
  106.  
  107.             return true; 
  108.  
  109.         }  
  110.  
  111.     }  
  112.  
  113.     // 协议法,因为有可能不准确,放到最后判断 
  114.  
  115.     if (isset ($_SERVER['HTTP_ACCEPT'])) 
  116.  
  117.     {  
  118.  
  119.         // 如果只支持wml并且不支持html那一定是移动设备 
  120.  
  121.         // 如果支持wml和html但是wml在html之前则是移动设备 
  122.  
  123.         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')))) 
  124.  
  125.         { 
  126.  
  127.             return true; 
  128.  
  129.         }  
  130.  
  131.     }  
  132.  
  133.     return false; 
  134.  
  135. }  
  136.  
  137. ?> 

国外人喜欢写类,有一个Mobile Detect,Mobile_Detect 简单使用实例:

  1. include 'Mobile_Detect.php'
  2.  
  3. $detect = new Mobile_Detect(); 
  4.  
  5.   
  6.  
  7. // Check for any mobile device. 
  8.  
  9. if ($detect->isMobile()) 
  10.  
  11.   
  12.  
  13. // Check for any tablet. 
  14.  
  15. if($detect->isTablet()) 
  16.  
  17.   
  18.  
  19. // Check for any mobile device, excluding tablets. 
  20.  
  21. if ($detect->isMobile() && !$detect->isTablet()) 
  22.  
  23.   
  24.  
  25. if ($detect->isMobile() && !$detect->isTablet()) 
  26.  
  27.   
  28.  
  29. // Alternative to $detect->isAndroidOS() 
  30.  
  31. $detect->is('AndroidOS'); 
  32.  
  33.   
  34.  
  35. // Batch usage 
  36.  
  37. foreach($userAgents as $userAgent){ 
  38.  
  39.   $detect->setUserAgent($userAgent); 
  40.  
  41.   $isMobile = $detect->isMobile(); 
  42.  //phpfensi.com 
  43.  
  44. // Version check. 
  45.  
  46. $detect->version('iPad'); // 4.3 (float)

Tags: PHP移动设备 isMobile

分享到: