_php instance of client type judgement based on PHP

Source: Internet
Author: User
Tags lenovo

In the website user data statistics, we often need to obtain the user browser type and the operating system type, this article mainly introduces is based on the PHP to judge the client type, has the need friend to be possible to understand.

Method One:

/** * To determine whether it belongs to the PC or the mobile end * @author discuz3x * @lastmodify According to the various browser-specific strings contained in PHP's $_server[' http_user_agent ')
 2014-04-09 * @return BOOL/function Checkmobile () {Global $_g;
$mobile = Array (); The array of strings contained in $_server[' http_user_agent ' in each touch browser is static $touchbrowser _list =array (' iphone ', ' Android ', ' phone ', ' Mobile ', ' wap ', ' NetFront ', ' Java ', ' opera Mobi ', ' Opera Mini ', ' UCWeb ', ' Windows CE ', ' Symbian ', ' series ', ' webOS ', ' so NY ', ' BlackBerry ', ' dopod ', ' Nokia ', ' Samsung ', ' PalmSource ', ' xda ', ' pieplus ', ' Meizu ', ' MIDP ', ' CLDC ', ' Motorola ', ' fo Ma ', ' DoCoMo ', ' up.browser ', ' up.link ', ' blazer ', ' helio ', ' hosin ', ' The ', ' Novarra ', ' Coolpad ', ' webOS ', ' TechFaith ' , ' PalmSource ', ' Alcatel ', ' Amoi ', ' Ktouch ', ' Nexian ', ' Ericsson ', ' Philips ', ' Sagem ', ' wellcom ', ' Bunjalloo ', ' Maui ', '
  Smartphone ', ' iemobile ', ' spice ', ' bird ', ' zte-', ' longcos ', ' Pantech ', ' Gionee ', ' portalmmm ', ' Jig browser ', ' Hiptop ',
' BenQ ', ' Haier ', ' ^lct ', ' 320x320 ', ' 240x320 ', ' 176x220 '; WiNdow Mobile Browser Array "Guessing" static $mobilebrowser _list =array (' Windows Phone '); $_server[' Http_user_agent ' in WAP browser contains an array of strings that are static $wmlbrowser _list = Array (' Cect ', ' compal ', ' ctl ', ' LG ', ' NEC ', ' Tcl ', ' Alcatel ', ' Ericsson ', ' bird ', ' Daxian ', ' Dbtel ', ' eastcom ', ' Pantech ', ' Dopod ', ' Philips ', ' Haier ', ' Konka ', ' Kej Ian ', ' 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[' mobile ' = $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 version} $brower = Array (' Mozilla ', ' Chrome ', ' Safari ', ' opera ', ' m3gate ', ' winwap ', ' Openwave ', ' myop '); if (dStrpos ($useragent, $brower)) return false;
$_g[' mobile ' = ' unknown ';
 For an unknown type of browser, the $_get[' mobile ' parameter is used to determine if it is a mobile browser if (Isset ($_g[' mobiletpl '][$_get[' mobile ')]) {return true;
 else {return false; }/** * Determines whether the $arr element string appears in $string * @param $string $_server[' http_user_agent '] * @param $arr browser $_server[' H Ttp_user_agent '] must contain the string * @param $returnvalue return the browser name or return a Boolean value, true to return the browser name, False to return the Boolean "default" * @author discuz3x *
 @lastmodify 2014-04-09 */function Dstrpos ($string, $arr, $returnvalue = False) {if (empty ($string)) return false;
  foreach ((array) $arr as $v) {if (Strpos ($string, $v)!== false) {$return = $returnvalue? $v: true;
 return $return;
return false;
 }

Method Two:

<?php function IsMobile () {//If http_x_wap_profile 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 of the service chambers will block the information if (Isset ($_server[' Http_via ')) {//Cannot find flase, otherwise return is true return Stri Str ($_server[' Http_via '], "WAP")?
  True:false;
      //Brain residue method, to judge the mobile phone to send the client flag, compatibility needs to be improved if (Isset ($_server[' http_user_agent ')) {$clientkeywords = array (' Nokia '), ' Sony ', ' Ericsson ', ' mot ', ' Samsung ', ' HTC ', ' SGH ', ' lg ', ' sharp ', ' sie-
      ', ' Philips ', ' Panasonic ', ' Alcatel ', ' Lenovo ', ' iphone ', ' ipod ', ' BlackBerry ', ' Meizu ', ' Android ', ' NetFront ', ' Symbian ', ' UCWeb ', ' windowsce ', ' palm ', ' 
      Operamini ', ' operamobi ', ' Openwave ', ' nexusone ', ' cldc ', ' MIDP ', ' wap ', ' mobile ' 
    ); Find the keyword if (preg_match) ("/") for the mobile 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 judge if (Isset ($_server[' http_accept ')) {//If only WML is supported and HTML is not supported it must be a mobile device//If supported W ML and HTML But WML is the mobile device before HTML (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:

/**
 * To determine whether the mobile phone 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;
}

Thank you for reading, I hope to help you, thank you for your support for this site!

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.