Determining client type based on PHP implementation

Source: Internet
Author: User
This article is mainly about PHP to determine whether the client is a PC or mobile phone-side method, you can easily use the site for statistics, need to know friends can refer to.

In the site user data statistics, we often need to get the type of user browser and operating system type, this article is mainly based on PHP to determine the client type, the need for friends can come to understand.

Method One:

/** * * Based on the various browser-specific strings included in PHP $_server[' http_user_agent ') to determine whether they belong to a PC or a mobile * @author discuz3x * @lastmodify 201 4-04-09 * @return BOOL */function checkmobile () {global $_g; $mobile = Array ();//$_server[' http_user_agent ' in each touch browser contains string array static $touchbrowser _list =array (' iphone ', ' Android ', ' phone ', ' mobile ', ' wap ', ' NetFront ', ' Java ', ' opera mobi ', ' O Pera Mini ', ' UCWeb ', ' Windows CE ', ' Symbian ', ' series ', ' webOS ', ' Sony ', ' BlackBerry ', ' dopod ', ' Nokia ', ' Samsung ', ' Pal Msource ', ' xda ', ' pieplus ', ' Meizu ', ' MIDP ', ' CLDC ', ' Motorola ', ' Foma ', ' DoCoMo ', ' up.browser ', ' up.link ', ' blazer ', ' he  Lio ', ' hosin ', ' Huawei ', ' Novarra ', ' Coolpad ', ' webOS ', ' techfaith ', ' palmsource ', ' Alcatel ', ' Amoi ', ' Ktouch ', ' Nexian ', ' Ericsson ', ' Philips ', ' Sagem ', ' wellcom ', ' Bunjalloo ', ' Maui ', ' smartphone ', ' iemobile ', ' spice ', ' bird ', ' zte-', ' long Cos ', ' Pantech ', ' Gionee ', ' portalmmm ', ' Jig browser ', ' hiptop ', ' BenQ ', ' Haier ', ' ^lct ', ' 320x320 ', ' 240x320 ', ' 176x220 ' );//window Mobile browser array "guess" staTic $mobilebrowser _list =array (' Windows Phone '), an array of strings contained in $_server[' Http_user_agent ' in//wap browser static $wmlbrowser _ List = Array (' Cect ', ' compal ', ' ctl ', ' LG ', ' nec ', ' tcl ', ' Alcatel ', ' Ericsson ', ' bird ', ' Daxian ', ' Dbtel ', ' eastcom ', ' p Antech ', ' Dopod ', ' Philips ', ' Haier ', ' Konka ', ' Kejian ', ' Lenovo ', ' BenQ ', ' mot ', ' Soutec ', ' Nokia ', ' Sagem ', ' sgh ', ' sed ', ' Capitel ', ' Panasonic ', ' SonyEricsson ', ' sharp ', ' amoi ', ' Panda ', ' ZTE '; $pad _list = array (' pad ', ' gt-p1000 '); $useragent = Strtolower ($_server[' http_user_agent '); if (Dstrpos ($useragent, $pad _list)) {return false;} if ($v = Dstrpos ($useragent, $mobilebrowser _list, True)) {$_g[' mobi Le '] = $v; Return ' 1 ';  } if ($v = Dstrpos ($useragent, $touchbrowser _list, True)) {$_g[' mobile '] = $v; return ' 2 ';} if (($v = Dstrpos ($useragent, $wmlbrowser _list)) {$_g[' mobile '] = $v; return ' 3 ';//WML} $brower = Array (' Mozilla ', ' Chrome ', ' Safari ', ' opera ', ' m 3gate ', ' winwap ', ' Openwave ', ' myop '); if (Dstrpos ($useragent, $brower)) return false;$_g[' mobile ' = ' unknown ';//for browsers of unknown type, use the $_get[' mobile ' parameter to determine if it is a mobile browser if (Isset ($_g[' mobiletpl '][$_get[' mobile ']) {return true;} else {return false;}} /** * Determine if the element string in $arr appears in $string * @param $string $_server[' http_user_agent '] * @param $arr browser $_server[' Http_use The string that must be contained in the R_agent '] @param $returnvalue return the browser name or return a Boolean value, true to return the browser name, False to return a Boolean value of "Default" * @author discuz3x * @lastmodif Y 2014-04-09 */function dstrpos ($string, $arr, $returnvalue = False) {if (empty ($string)) return false; foreach (array) $a  RR as $v) {if (Strpos ($string, $v)!== false) {$return = $returnvalue? $v: true; return $return; }} return false;}

Method Two:

<?phpfunction IsMobile () {//If there is a http_x_wap_profile, it must be a mobile device if (Isset ($_server[' http_x_wap_profile ')) {return  True }//If the VIA information contains WAP it must be a mobile device, some service associations block the information if (Isset ($_server[' Http_via ')) {//Cannot find for flase, otherwise true return Stristr ($ _server[' Http_via '], "WAP")?  True:false; }//Brain residue method, determine the mobile phone sent by the client flag, compatibility needs to be improved if (Isset ($_server[' http_user_agent ')) {$clientkeywords = array (' Nokia ', ' so NY ', ' Ericsson ', ' mot ', ' Samsung ', ' HTC ', ' SGH ', ' lg ', ' sharp ', ' sie-', ' Phil IPs ', ' Panasonic ', ' Alcatel ', ' Lenovo ', ' iphone ', ' ipod ', ' blackberry ', ' Meizu ', ' Android ', ' NetFront ', ' Symbian ', ' UCWeb ', ' windowsce ', ' palm ', ' operamini ', ' Operamobi '     , ' Openwave ', ' nexusone ', ' cldc ', ' MIDP ', ' wap ', ' mobile '; Look for the keyword if (Preg_match ("/(") of the phone's browser from Http_user_agent. Implode (' | ', $clientkeywords). ")/I", Strtolower ($_server[' Http_user_agent '])) {return true; }}//protocol method, because it may not be accurate, put to the Last Judgment if (Isset ($_server[' http_accept ')) {//If only WML is supported and HTML is not supported that must be mobile device//If WML and HTM are supported L but WML before HTML is the mobile device 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 '))))    {return true; }} return false;} ?>

Determine if the phone is iOS or Android

The code is as follows:

/** * Determine if mobile Access */function Is_mobile () {  static $is _mobile;  if (Isset ($is _mobile)) return $is _mobile;  if (Empty ($_server[' http_user_agent ')) {    $is _mobile = false;  } elseif (Strpos ($_server[' http_user_agent '], ' Mobile ')!== false//Many mobile devices (all IPhone, IPad, etc)    | | Strpos ($_server[' http_user_agent '), ' Android ')!== false    | | strpos ($_server[' http_user_agent '], ' silk/')!== False    | | strpos ($_server[' http_user_agent '), ' Kindle ')!== false    | | strpos ($_server[' http_user_agent '], ' BlackBerry ')!== false    | | strpos ($_server[' http_user_agent '), ' Opera Mini ')!== false    | | strpos ($_server[' Http_user_agent '], ' Opera Mobi ')!== false  ) {    $is _mobile = true;  } else {    $is _mobile = false;  }  return $is _mobile;}

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.