Home >Backend Development >PHP Tutorial >PHP determines whether the visitor is a mobile client instance_PHP tutorial

PHP determines whether the visitor is a mobile client instance_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:49:08903browse

Mobile Internet has become very popular recently. We need to build a PC site and a WAP site. To realize that if the user accesses the WAP site from a computer, he will automatically enter the PC site, and vice versa. I have compiled some codes below for you to take a look at.

Method 1: Determine HTTP_USER_AGENT

The code is as follows Copy code
 代码如下 复制代码

$agent = strtolower($_SERVER['HTTP_USER_AGENT']); 
if(strpos($agent,"netfront") || strpos($agent,"iphone") || strpos($agent,"midp-2.0") || strpos($agent,"opera mini") || strpos($agent,"ucweb") || strpos($agent,"android") || strpos($agent,"windows ce") || strpos($agent,"symbianos")) {
    Header("HTTP/1.1 301 Moved Permanently");
    header("Location:####");  die;
}

$agent = strtolower($_SERVER['HTTP_USER_AGENT']);

if(strpos($agent,"netfront") || strpos($agent,"iphone") || strpos($agent,"midp-2.0") || strpos($agent,"opera mini") || strpos ($agent,"ucweb") || strpos($agent,"android") || strpos($agent,"windows ce") || strpos($agent,"symbianos")) {
Header("HTTP/1.1 301 Moved Permanently");

header("Location:####"); die;
 代码如下 复制代码
if (isset($_SERVER['HTTP_ACCEPT']) && (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')) )) {//手机访问
Header("HTTP/1.1 301 Moved Permanently");
header("Location:####"); die;
}
}<🎜>
<🎜><🎜> Method 2: Determine HTTP_ACCEPT<🎜>
The code is as follows Copy code
if (isset($_SERVER['HTTP_ACCEPT']) && (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')) )) {//Mobile access <🎜> Header("HTTP/1.1 301 Moved Permanently"); <🎜> header("Location:####"); die; <🎜> }

The above two methods have limitations,

The following two methods are combined to determine

The code is as follows Copy code

function isMobile() {
If(isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
         return true;
}
If(isset ($_SERVER['HTTP_VIA'])) {
//If not found, it is false, otherwise it is true
          return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
}
If(isset($_SERVER['HTTP_USER_AGENT'])) {
//This array needs to be improved
           $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 keywords for mobile browsers from HTTP_USER_AGENT
If(preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
              return true;
          }

}

// Agreement method, because it may be inaccurate, leave it to the final judgment
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 comes before html, it is a 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;
}

The above method also has some minor problems. Here I tell you based on my own experience that we can use the screen width to implement and add the machine type, because sometimes the HTTP_USER_AGENT information is not defined above, but the above implementation It is almost compatible with mainstream mobile phones.

We can also use js


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632726.htmlTechArticleMobile Internet has become very popular recently. We need to build a PC site and a WAP site to realize that if the user accesses the WAP site from a computer It will automatically enter the PC station, and vice versa. Below I have compiled some codes and big...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn