How can I determine whether the access end is a computer or a mobile phone? _ PHP Tutorial

Source: Internet
Author: User
Tags php example website server perl script
How can I determine whether the access end is a computer or a mobile phone ?. Currently, there are three opinions on the Internet: one is the User-Agent sent based on the browser, but this method obviously does not work. although some people list the User-Agent sent by most mobile phones, however, there are three opinions on the Internet: one is the User-Agent sent based on the browser, but obviously this method does not work. although some people list the User-Agent sent by most mobile phones, however, there are still many mobile phones that cannot be identified. some mobile browsers do not even send User-Agent messages, and they cannot guarantee that new brands will not be available in the future.

The following 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 is to judge HTTP_ACCEPT, which should be relatively reliable, but HTTP_ACCEPT is very complicated. If html browsers are generally not supported, you only need to judge that the browser supports wml and does not support html. However, if the browser supports both wml and html, it will be difficult. The low-end mobile phone may put wml in front of html, however, many high-end mobile phones or smart phones support html well, 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. J2. 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 view IE or FF HTTP_ACCEPT, you will find it very short.

Text/html,

Application/xhtml + xml,

Application/xml; q= 0.9,

*/*; Q = 0.8

Let me give you a php example.

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 [HTML is not supported], or [WML takes precedence over HTML], it is determined as a mobile phone. But in fact this principle is incorrect.

Another method is to determine the Gateway or IP address, but I personally think this is very unreliable and there are many ways to connect to the Internet on mobile phones. for example, I used a simulator during development. no example is needed. REMOTE_ADDR is enough.

The above are my work achievements this morning, but I still have not found a perfect solution. I have written so many articles first. if you have any questions, please add them or send me a message, if you have a better way to share it with me, thank you.

A mobile phone query system recently involved this issue. I will discuss how to use php to determine whether a user can access the system through wap or a computer.

First, the most fundamental solution:

The user-agent information is sent along with the mobile phone number information. if the mobile phone number can be obtained, it must be accessed through mobile wap. However, China Mobile has blocked the user-agent information, so the mobile phone number cannot be obtained. You can contact a mobile company to submit the ip address of the wap website server to China Mobile. after adding the ip address to the whitelist, you can obtain the ua Information. Currently, China Unicom can directly obtain the mobile phone number, which is perfect for China Unicom users.

My solution is as follows:

The principle of mobile phone access is that the mobile phone is accessed through the proxy server of the mobile company. Then we can understand that a common computer uses a proxy server. When a mobile phone is accessed through a proxy server, the http header will undoubtedly contain a message:. 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 of other provinces is similar to this one. The scheme to determine whether a mobile phone can be accessed comes out: obtain the http via information string to check whether the string contains wap characters. If yes, it is accessed through a mobile phone. The result is that no one can forge a mobile phone to access the website, and the judgment is absolutely accurate. Naturally, this protects the mobile phone wap simulators that are popular on the internet-fundamentally.

The operation code is also simple:

// Check if wap by xhat

Function check_wap (){

Return stristr ($ _ SERVER ['http _ vean'], "wap ")? True: false;

}

// Check over

According to the materials I have consulted, this method should be the most accurate and simplest method for judging mobile phone access on the Internet so far.

The above method can be used to determine whether the access source is a mobile phone or a computer, but one problem is that the access can only be made through the mobile phone, and the WAP-supported browser and mobile phone simulator cannot be accessed, this causes a lot of trouble in testing, and it is also a little restricted.

The following solution is as follows:

Determine the browser proxy identifier and determine whether the browser supports WAP to access the page. The disadvantage is that it is impossible to list all the mobile phone's browser identifiers and supported browser identifiers.

Let's take a look at the implementation 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.wapja.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 = "courier") // Opera

{

$ Br = "WML ";

}

Else

{

$ Br = "HTML ";

}

}

If ($ br = "WML ")

{

Return TRUE;

}

Else

{

Return FALSE;

}

}

If (! Check_wap ())

{

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

Exit ();

}

Other, one is the User-Agent sent based on the browser, but obviously this method does not work, although some people list the User-Agent sent by most mobile phones, but still...

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.