How can I tell if the access end is a computer or a cell phone?

Source: Internet
Author: User
Tags readfile perl script

There are now three views on the Internet, one is based on the user-agent sent by the browser, but obviously this method is not feasible, although some people have listed the most mobile phone sent user-agent, but there will still be a lot of mobile phones are not recognized, and even some mobile browsers do not send User-agent, And there is no guarantee that there will be no new brand in the future.

Here's an example of PHP, and you'll know how unreliable it is after you look at it.

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 complex, if the general HTML-not-supported browsers are okay to say, you just have to judge that browsers support WML and do not support HTML. But if browsers support WML and HTML at the same time, it's tricky, low-end phones may put WML in front of HTML, but many high-end phones or smartphones are good for HTML, so HTML will 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

Are you exaggerating? But it's really standard, developers can get a lot of information based on this, but a lot of mobile phones are not so standard, if you look at IE or FF http_accept you will find it very short.

Text/html,

Application/xhtml+xml,

application/xml;q=0.9,

*/*;q=0.8

Again, 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 ');

Rationale: If the browser supports WML and [does not support HTML] or [WML overrides HTML], the cell phone is judged. But in practice this principle is not correct.

Another is to say that Judge Gateway or IP, but I personally think this is very unreliable, mobile phone networking way a lot, such as I developed the time is to use the simulator. This should not be an example, REMOTE_ADDR is enough.

These are the results of my work this morning, but actually still did not find the perfect solution, write so many, have questions please add or send a message to me, if you have a better way to hope to share to me, thank you.

Recently made a mobile phone query system, naturally involved in this issue, then I will be based on the understanding of WAP on the basis of PHP to determine the user access through the WAP access or computer direct access.

First of all, the most fundamental solution:

Mobile phone access, will be accompanied by the sending of user-agent information, this information will have mobile phone number information, so if you can get mobile phone number, it can be definitely through WAP access to mobile phones. But at present China Mobile has blocked the user-agent information, therefore obtains the handset number. Related friends can contact the mobile company, the WAP Web server IP submitted to China Mobile, add the whitelist can obtain UA information. At present, China Unicom can take the mobile phone number directly to the Unicom users of this scheme can be implemented perfectly.

Next, my solution:

Mobile phone access, the principle is mobile phone through the mobile company's proxy server access. Then we can understand that an ordinary computer uses a proxy server. When a mobile phone is accessed through a proxy server, the HTTP header information will undoubtedly contain a message: via. This information provides valuable judgment information.

For example, the Henan mobile obtained via information is:

http/1.1 Hazz-b-gw001-wap (INFOX-WISG, technologies)

The VIA information of Henan Unicom is:

Zxwap Gateway,zte Technologies

Other provinces of HTTP headers and this very similar, to determine whether the mobile phone access scheme is out: Get the HTTP via information string to see if there are WAP characters, if it is through mobile phone access. The result is that no one can fake a mobile phone interview, and the judgment is absolutely accurate. Naturally, this is the Internet's popular mobile WAP simulator has also been shielded-fundamentally shielded.

The operation code is also simple:

Check if WAP by Xhat

function Check_wap () {

Return Stristr ($_server[' Http_via '], "WAP")? True:false;

}

Check over

According to the information I have consulted, this method should be the most accurate and simple method of judging the access to mobile phone so far on the internet.

The above method can be used to determine whether the access source is a mobile phone or a computer, but there is a problem, only through the mobile phone to access, WAP-enabled browsers and mobile phone simulator can not access, which brought trouble to the test, but also a bit limited to death.

The following scenarios are:

By judging the browser proxy identifier, determine whether it is a WAP-enabled browser to determine access to the page. One drawback is that it's not possible to list all the browser identifiers for all your phones and the browser identifier you support.

Here's 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.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 ();

}

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.