PHP site to determine whether the user is a mobile phone access method _php instance

Source: Internet
Author: User

With the popularity of mobile devices, the site will also usher in more and more mobile devices access. Used to adapt to the PC page, often unfriendly to mobile phone users, then sometimes, we need to determine whether the user to access the phone, if it is a mobile phone, jump to the designated mobile friendly page. Here is a brief introduction, how to determine whether users use mobile phone access.

The custom functions are as follows:

$agent = Check_wap ();
if ($agent)
{
  header (' location:http://www.jb51.net ');
  Exit;
}
 
Check if WAP 
function Check_wap () {
  ///Checking for WAP proxy first, high accuracy
  if (stristr ($_server[' Http_via '), "WAP") { return
    true;
  }
  Check to see if the browser accepts WML.
  ElseIf (Strpos strtoupper ($_server[' http_accept ')), "VND." Wap. WML ") > 0) {return
    true;
  }
  Check user_agent
  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 ']) {return
    true;       
  }
  else{return
    false;  
  }

A function that is stripped from the PHP frame to determine if it is a mobile terminal: (tested, very comprehensive, recommended for use)

 function is_mobile_request () {$_server[' all_http '] = isset ($_server[' all_http '])?  
 $_server[' all_http ']: ';  
 $mobile _browser = ' 0 '; if (Preg_match ('/up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/I ',  
 Strtolower ($_server[' http_user_agent '))) $mobile _browser++; if (Isset ($_server[' http_accept ')) and (Strpos strtolower ($_server[' http_accept ')), ' application/vnd.wap.xhtml+  
 XML ')!== false) $mobile _browser++;  
 if (Isset ($_server[' http_x_wap_profile ')) $mobile _browser++;  
 if (Isset ($_server[' http_profile ')) $mobile _browser++;  
 $mobile _ua = Strtolower (substr ($_server[' http_user_agent '],0,4)); $mobile = Array (' _agents ', ' acs-', ' Alav ', ' Alca ', ' amoi ', ' Audi ', ' Avan ', ' BenQ ', ' bird ', ' Blac ', ' Blaz ', ' Brew ', ' Cell ', ' cldc ', ' cmd-', ' Dang ', ' doco ', ' Eric ', ' Hipt ', ' Inno ', ' iPAQ ', ' Java ', ' Jigs ', ' kddi ', ' Keji ', ' Leno ', ' lg-c ', ' lg-d ' , ' lg-g ', ' lge-', ' Maui ', ' Maxo ', ' MIDP ', ' mits ', ' mMEF ', ' mobi ', ' mot-', ' moto ', ' mwbp ', ' nec-', ' Newt ', ' Noki ', ' oper ', ' palm ', ' pana ', ' Pant ', ' Phil ', ' play ', ' Port ', ' ProX ', ' Qwap ', ' sage ', ' Sams ', ' Sany ', ' sch-', ' sec-', ' send ', ' Seri ', ' sgh-', ' shar ', ' sie-', ' Siem ', ' smal ', ' Smar ', ' Sony ', ' sph-', ' symb ', ' t-mo ', ' Teli ', ' tim-', ' tosh ', ' tsm-', ' upg1 ', ' upsi ', ' vk-v ', ' Voda ', ' wap-', ' wapa ', ' wapi ', ' Wapp ', ' wap  
 R ', ' Webc ', ' winw ', ' winw ', ' xda ', ' xda-');  
 if (In_array ($mobile _ua, $mobile _agents)) $mobile _browser++;  
 if (Strpos (Strtolower ($_server[' all_http ')), ' Operamini ')!== false) $mobile _browser++; pre-final Check to reset everything if the user was on Windows if (Strpos (Strtolower ($_server[' http_user_agent ')), ' WI  
 Ndows ')!== false) $mobile _browser=0;  But WP7 is also Windows, with a slightly different characteristic if (Strpos (strtolower ' $_server[')),  
 ' Windows Phone ')!== false) $mobile _browser++;  
 if ($mobile _browser>0) return true;  else return false; 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.