Home > Backend Development > PHP Tutorial > How does PHP determine whether the user accesses through the mobile wap or directly from the computer_PHP tutorial

How does PHP determine whether the user accesses through the mobile wap or directly from the computer_PHP tutorial

WBOY
Release: 2016-07-13 10:44:12
Original
1202 people have browsed it

Nowadays, we have a lot of smart phone users. When visiting the website, many friends use their mobile phones to access the website directly. This is a problem. If it is a PC version, the mobile phone access will definitely not look good and consume people’s traffic. Therefore, many companies have built WAP sites, but How to determine whether the user is accessing the website from a mobile phone or a PC? Below I have compiled some methods.

Recently I was working on a mobile phone query system, which naturally involved this issue. Then I will use PHP to determine whether the user access method is through WAP access or direct computer access based on my understanding of WAP.
Let’s talk about the most fundamental solution first:
When accessing via mobile phone, user-agent information will be sent along with it. This information will contain mobile phone number information. If you can obtain the mobile phone number, you can definitely access it through mobile wap. However, China Mobile has currently blocked user-agent information, so the mobile phone number cannot be obtained. Friends who are related can contact the mobile company and submit the IP of the WAP website server to China Mobile. After adding it to the whitelist, the UA information can be obtained. At present, China Unicom can directly obtain mobile phone numbers, and this solution can be implemented perfectly for Unicom users.
Next, let’s talk about my solution:
The principle of mobile phone access is that the mobile phone accesses through the proxy server of the mobile company. Then we can understand that an ordinary computer uses a proxy server. When a mobile phone accesses through a proxy server, the http header information will undoubtedly contain a piece of information: via. This information provides valuable judgment information.
For example, the via information obtained by Henan Mobile is:
http/1.1 hazz-b-gw001-wap(infox-wisg, huawei technologies)
Henan Unicom’s via information is:
zxwap gateway,zte technologies
The http header information in other provinces is similar to this. The solution to determine whether mobile phone access is available: Get the via information string of http to see if it contains wap characters. If so, access via mobile phone. The result of this is that no one can fake mobile phone access and the judgment is absolutely accurate. Naturally, this also blocks the popular mobile phone wap simulators on the Internet - fundamentally blocking them.
The operation code is also very simple:

The code is as follows Copy code
// check if wap by xhat
 代码如下 复制代码
// check if wap by xhat
function check_wap() {
return stristr($_SERVER['HTTP_VIA'],"wap") ? true : false;
}
// check over
function check_wap() { return stristr($_SERVER['HTTP_VIA'],"wap") ? true : false; } // check over

From the information I have reviewed, this method should be the most accurate and simplest way to determine mobile phone access on the Internet so far.

iphone smartphone

The iPhone version for discuz is basically completed. In order to facilitate access, the iPhone access is directly judged on the homepage, and then jumps directly

Using the above code to judge, the test results are very good.

A judgment class that I use, which is more comprehensive

 代码如下 复制代码

/*
 判断访问用户是否为手机bKjia.c0m
 //判断是否属手机
 */
 function is_mobile() {
  $user_agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  //echo $user_agent;
  $mobile_agents = Array("ipad","wap","android","iphone","sec","sam","ericsson","240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte","ben","hai","phili");
  $is_mobile = false;
  foreach ($mobile_agents as $device) {
   if (stristr($user_agent, $device)) {
    if( 'ipad' == $device )
    {
     return $is_mobile;
    }
    $is_mobile = true;
    break;
   }
  }
  return $is_mobile;
 }

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633111.htmlTechArticleNow we have a lot of smart phone users. When visiting the website, many friends use their mobile phones to access it directly. This is a problem. If it is The PC version will definitely not look good when accessed via mobile phone and it will cost people’s data...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template