WAP 网站判断是否是使用手机访问的PHP 代码

这段PHP代码用于检测用户是否使用手机访问WAP网站,通过检查浏览器类型、接受内容类型、HTTP头等信息,匹配多种常见移动设备,包括iPhone、iPad、Android、Opera、Blackberry、Palm和Windows Mobile等。代码包含两个函数mobile_device_detect()和is_mobile(),具有高匹配率。
摘要由CSDN通过智能技术生成

function mobile_device_detect($iphone = true, $ipad = true, $android = true, $opera = true, $blackberry = true, $palm = true, $windows = true, $mobileredirect = false, $desktopredirect = false) {
 
 $mobile_browser = false;
 $user_agent = $_SERVER ['HTTP_USER_AGENT'];
 $accept = $_SERVER ['HTTP_ACCEPT'];
 
 switch (true) {
  
  case (preg_match ( '/ipad/i', $user_agent )) :
   $mobile_browser = $ipad;
   $status = 'Apple iPad';
   if (substr ( $ipad, 0, 4 ) == 'http') {
    $mobileredirect = $ipad;
   }
   break;
  
  case (preg_match ( '/ipod/i', $user_agent ) || preg_match ( '/iphone/i', $user_agent )) : // we find the words iphone or ipod in the user agent
   $mobile_browser = $iphone; // mobile browser is either true or false depending on the setting of iphone when calling the function
   $status = 'Apple';
   if (substr ( $iphone, 0, 4 ) == 'http') {
    $mobileredirect = $iphone;
   }
   break;
  
  case (preg_match ( '/android/i', $user_agent )) :
   $mobile_browser = $android;
   $status = 'Android';
   if (substr ( $android, 0, 4 ) == 'http') {
    $mobileredirect = $android;
   }
   break;
  
  case (preg_match ( '/opera mini/i', $user_agent )) :
   $mobile_browser = $opera;
   $status = 'Opera';
   if (substr ( $opera, 0, 4 ) == 'http') {
    $mobileredirect = $opera;
   }
   break;
  
  case (preg_match ( '/blackberry/i', $user_agent )) :
   $mobile_browser = $blackberry;
   $status = 'Blackberry';
   if (substr ( $blackberry, 0, 4 ) == 'http') {
    $mobileredirect = $blackberry;
   }
   break;
  
  case (preg_match ( '/(pre\/|palm os|palm|hiptop|avantgo|plucker|xiino|blazer|elaine)/i', $user_agent )) :
   $mobile_browser = $palm;
   $status = 'Palm';
   if (substr ( $palm, 0, 4 ) == 'http') {
    $mobileredirect = $palm;
   }
   break;
  
  case (preg_match ( '/(iris|3g_t|windows ce|opera mobi|windows ce; smartphone;|windows ce; iemobile)/i', $user_agent )) : // we find windows mobile in the user agent - the i at the end makes it case insensitive
   $mobile_browser = $windows;
   $status = 'Windows Smartphone';
   if (substr ( $windows, 0, 4 ) == 'http') {
    $mobileredirect = $windows;
   }
   break;
  
  case (preg_match ( '/(mini 9.5|vx1000|lge |m800|e860|u940|ux840|compal|wireless| mobi|ahong|lg380|lgku|lgu900|lg210|lg47|lg920|lg840|lg370|sam-r|mg50|s55|g83|t66|vx400|mk99|d615|d763|el370|sl900|mp500|samu3|samu4|vx10|xda_|samu5|samu6|samu7|samu9|a615|b832|m881|s920|n210|s700|c-810|_h797|mob-x|sk16d|848b|mowser|s580|r800|471x|v120|rim8|c500foma:|160x|x160|480x|x640|t503|w839|i250|sprint|w398samr810|m5252|c7100|mt126|x225|s5330|s820|htil-g1|fly v71|s302|-x113|novarra|k610i|-three|8325rc|8352rc|sanyo|vx54|c888|nx250|n120|mtk |c5588|s710|t880|c5005|i;458x|p404i|s210|c5100|teleca|s940|c500|s590|foma|samsu|vx8|vx9|a1000|_mms|myx|a700|gu1100|bc831|e300|ems100|me701|me702m-three|sd588|s800|8325rc|ac831|mw200|brew |d88|htc\/|htc_touch|355x|m50|km100|d736|p-9521|telco|sl74|ktouch|m4u\/|me702|8325rc|kddi|phone|lg |sonyericsson|samsung|240x|x320|vx10|nokia|sony cmd|motorola|up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|psp|treo)/i', $user_agent )) : // check if any of the values listed create a match on the user agent - these are some of the most common terms used in agents to identify them as being mobile devices - the i at the end makes it case insensitive
   $mobile_browser = true;
   $status = 'Mobile matched on piped preg_match';
   break;
  
  case ((strpos ( $accept, 'text/vnd.wap.wml' ) > 0) || (strpos ( $accept, 'application/vnd.wap.xhtml+xml' ) > 0)) : // is the device showing signs of support for text/vnd.wap.wml or application/vnd.wap.xhtml+xml
   $mobile_browser = true;
   $status = 'Mobile matched on content accept header';
   break;
  
  case (isset ( $_SERVER ['HTTP_X_WAP_PROFILE'] ) || isset ( $_SERVER ['HTTP_PROFILE'] )) : // is the device giving us a HTTP_X_WAP_PROFILE or HTTP_PROFILE header - only mobile devices would do this
   $mobile_browser = true;
   $status = 'Mobile matched on profile headers being set';
   break;
  
  case (in_array ( strtolower ( substr ( $user_agent, 0, 4 ) ), array ('1207' => '1207', '3gso

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值