Home  >  Article  >  Backend Development  >  How to determine whether the access terminal is a computer or a mobile phone?_PHP tutorial

How to determine whether the access terminal is a computer or a mobile phone?_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:35:011547browse

There are currently three opinions on the Internet. One is based on the User-Agent sent by the browser, but obviously this method does not work. Although some people have listed the User-Agent sent by most mobile phones, but There are still many mobile phones that cannot be recognized, and some mobile browsers even do not send User-Agent, and there is no guarantee that new brands will not be released in the future.

Below is an example of PHP. After reading it, you will know how unreliable it is.

 1. function is_wap(){

2. $ua = strtolower($_SERVER['HTTP_USER_AGENT']);

 3. $uachar = "/(nokia|sony|ericsson|mot|samsung|sgh|lg|sie|philips|panasonic|alcatel|lenovo|cldc|midp|wap|mobile)/i";

 4. if(($ua == '' || preg_match($uachar, $ua))&& !strpos(strtolower($_SERVER['REQUEST_URI']),'wap')){

5. return true;

 6. }else{

7. return false;

 8. }

 9. }

The other method is to judge HTTP_ACCEPT, which should be relatively reliable, but HTTP_ACCEPT is very complicated. If the general browser does not support html, it is easy to say. You only need to judge that the browser supports wml and does not support html. But if the browser supports both wml and html, it will be difficult. Low-end mobile phones may put wml in front of html, but many high-end mobile phones or smart phones have good support for html, so html will also appear in front of wml. .

This is the HTTP_ACCEPT sent by the BlackBerry

Application/vnd.rim.html,

text/html,

Application/vnd.wap.xhtml+xml,

text/vnd.sun.j2me.app-descriptor,

 image/vnd.rim.png,image/jpeg,

Application/x-vnd.rim.pme.b,

Application/vnd.rim.ucs,

Image/gif;anim=1,

Application/vnd.rim.jscriptc;v=0-8-8,

Application/x-javascript,

Application/vnd.rim.css;v=1,

text/css;media=handheld,

Application/vnd.wap.wmlc;q=0.9,

Application/vnd.wap.wmlscriptc;q=0.7,

text/vnd.wap.wml;q=0.7,

 */*;q=0.5

Exaggerated? But it is indeed very standard. Developers can get a lot of information based on this, but many mobile phones are not so standard. If you check the HTTP_ACCEPT of IE or FF, you will find that it is very short.

text/html,

Application/xhtml+xml,

Application/xml;q=0.9,

 */*;q=0.8

Similarly, let me give you an example of php.

 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

readfile('index.wml');

 } else readfile('index.htm');

Principle: If the browser supports WML and [does not support HTML] or [WML takes precedence over HTML], it is determined to be a mobile phone. But in fact this principle is incorrect.

There is another way to determine the gateway or IP, but I personally think this is very unreliable. There are many ways to connect to mobile phones. For example, when I was developing, I used an emulator. There should be no examples for this, REMOTE_ADDR is Enough.

The above is the result of my work this morning, but in fact I still haven’t found a perfect solution. I’m just writing this much. If you have any questions, please add them or send me a message. If you have a better way, I hope you can share it with me. , thank you.

Recently I was working on a mobile phone query system, which naturally involved this issue. Then I will use PHP to judge 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. 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.

Now 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)

The via information of Henan Unicom is:

 zxwap gateway,zte technologies

The http header information in other provinces is similar to this. Here is a solution to determine whether mobile access is possible: Get the via information string of http to see if it contains wap characters. If so, it is accessed 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:

 // check if wap by xhat

Function check_wap() {

return stristr($_SERVER['HTTP_VIA'],"wap") ? true : false;

 }

// check over

Judging 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.

The above method can determine whether the access source is a mobile phone or a computer, but one problem is that it can only be accessed through a mobile phone, and browsers and mobile phone simulators that support WAP cannot be accessed, which brings trouble to the test. It’s also a bit restrictive.

The following plan is:

Determine the access page by judging the browser proxy identifier and whether it is a browser that supports WAP. One disadvantage of this is that it is impossible to list all the browser identifiers and supported browser identifiers of all mobile phones.

Let’s take a look at the implemented code:

Function check_wap()

 {

 if (strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"VND.WAP.WML") > 0)

 {

// Check whether the browser/gateway says it accepts WML.

 $br = "WML";

 }

else

 {

$browser=substr(trim($_SERVER['HTTP_USER_AGENT']),0,4);

 if ($browser=="Noki" || // Nokia phones and emulators

 $browser=="Eric" || // Ericsson WAP phones and emulators

 $browser=="WapI" || // Ericsson WapIDE 2.0

 $browser=="MC21" || // Ericsson MC218

 $browser=="AUR" || // Ericsson R320

 $browser=="R380" || // Ericsson R380

 $browser=="UP.B" || // UP.Browser

 $browser=="WinW" || // WinWAP browser

 $browser=="UPG1" || // UP.SDK 4.0

 $browser=="upsi" || // another kind of UP.Browser ??

 $browser=="QWAP" || // unknown QWAPPER browser

 $browser=="Jigs" || // unknown JigSaw browser

 $browser=="Java" || // unknown Java based browser

 $browser=="Alca" || // unknown Alcatel-BE3 browser (UP based?)

 $browser=="MITS" || // unknown Mitsubishi browser

 $browser=="MOT-" || // unknown browser (UP based?)

 $browser=="My S" ||// unknown Ericsson devkit browser ?

 $browser=="WAPJ" || // Virtual WAPJAG www.wapjag.de

 $browser=="fetc" || // fetchpage.cgi Perl script from www.wapcab.de

 $browser=="ALAV" || // yet another unknown UP based browser ?

 $browser=="Wapa" || // another unknown browser (Web based "Wapalyzer"?)

 $browser=="Oper") // Opera

 {

 $br = "WML";

 }

else

 {

 $br = "HTML";

 }

 }

 if($br == "WML")

 {

return TRUE;

 }

else

 {

return FALSE;

 }

 }

 if(!check_wap())

 {

Header("Location: http://www.xker.com/");

exit();

 }

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/747073.htmlTechArticleThere are three opinions on the Internet. One is based on the User-Agent sent by the browser, but obviously this method is It doesn't work, although someone has listed the User-Agent sent by most mobile phones, but still...
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