Summary of PHP’s methods for determining whether visitors visit mobile (mobile browser) [4 methods]

Time:2020-5-15

This paper summarizes PHP’s method of judging whether visitors visit on mobile browser. To share with you for your reference, as follows:

In the normal work development, we usually need to develop two different systems, PC and mobile, so as to enter into different operation interfaces according to the different clients. This requires us to judge whether the client is PC or mobile.

This post discusses four ways to use PHP to determine whether the client is a mobile (or a mobile browser) to determine whether it is a mobile access:

1. Judge whether the HTTP ﹣ WAP ﹣ profile exists. If it exists, it is the mobile terminal (some services will block the information)

/**
*Is it mobile access
*@ desc judge whether it is mobile terminal to access
*@ method 1: judge whether there is http_ X_ WAP_ Profile, if any, it must be a mobile device.
* @return bool
* $Author: Zhihua_W
*/
function isMobile1()
{
   if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) {
     return true;
   } else {
     return false;
   }
}

2. Judge according to the http_via information. If the via information contains WAP, it is a mobile device (some services will block the information)

/**
 *Is it mobile access
 *@ desc judge whether it is mobile terminal to access
 *@ method 2: judge whether the http_via information contains WAP information. If yes, it must be a mobile device.
 * @return bool
 * $Author: Zhihua_W
 */
 function isMobile2 ()
 {
   if (isset ($_SERVER['HTTP_VIA'])) {
     return true;
   } else {
     return false;
   }
 }

3. According to the client logo sent by the mobile phone, this method is relatively stupid, and the compatibility needs to be improved

/**
*Is it mobile access
*@ desc judge whether it is mobile terminal to access
*@ method 3: judge whether there is an HTTP ﹣ user ﹣ agent message that is the client mark sent by the mobile phone. If there is one, it must be a mobile device.
* @return bool
* $Author: Zhihua_W
*/
function isMobile3 ()
{
   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'
      );
    //From http_ USER_ Search keywords of mobile browser in agent
    if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))){
      return true;
    } else {
      return false;
    }
  } else {
    return false;
  }
}

4. According to the protocol, this method may not be correct or can not be judged

/**
*Is it mobile access
*@ desc judge whether it is mobile terminal to access
*@ method 4: judge HTTP accept information
* @return bool
* $Author: Zhihua_W
*/
function isMobile4()
{
  if (isset ($_SERVER['HTTP_ACCEPT'])) {
    //If it only supports WML and does not support HTML, it must be a mobile device
    //If WML and HTML are supported, but WML is a mobile device before HTML
    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;
    } else {
    return false;
    }
  } else {
    return false;
  }
}

All of the above four methods have some defects and poor compatibility, so Boyou can combine the four methods into one method for judgment.

PS: Here we recommend 2 browser judgment online tools with similar functions for your reference:

Online browser information detection tool:
http://tools.jb51.net/aideddesign/browser_info

Common browser (PC, mobile) user agent:
http://tools.jb51.net/table/useragent

For more information about PHP, please refer to the following topics: summary of PHP network programming skills, summary of PHP regular expression usage, and PHP Curl usage summary, PHP array operation skill collection, PHP string usage summary, PHP data structure and algorithm tutorial, PHP programming algorithm summary, PHP mathematical operation skill summary and PHP common database operation skill summary

I hope this article is helpful for PHP programming.