Home Backend Development PHP Tutorial 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

Jul 13, 2016 am 10:35 AM
judgment send based on how cell phone yes Browser Now computer of end View access still

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem of third-party interface returning 403 in Node.js environment? How to solve the problem of third-party interface returning 403 in Node.js environment? Mar 31, 2025 pm 11:27 PM

Solve the problem of third-party interface returning 403 in Node.js environment. When we use Node.js to call third-party interfaces, we sometimes encounter an error of 403 from the interface returning 403...

gate.io registration tutorial gate.io registration tutorial Mar 31, 2025 pm 11:09 PM

This article provides a detailed Gate.io registration tutorial, covering every step from accessing the official website to completing registration, including filling in registration information, verifying, reading user agreements, etc. The article also emphasizes security measures after successful registration, such as setting up secondary verification and completing real-name authentication, and gives tips from beginners to help users safely start their digital asset trading journey.

Where to download the genuine Ouyi? Official website download. Global Where to download the genuine Ouyi? Official website download. Global Mar 31, 2025 pm 02:09 PM

Ouyi usually refers to Ouyi OKX. The global way to download Ouyi OKX APP is as follows: 1. Android device: Download the APK file through the official website and install it. 2. iOS device: access the official website through the browser and directly download the APP.

okx Ouyi Exchange web version enter link click to enter okx Ouyi Exchange web version enter link click to enter Mar 31, 2025 pm 06:21 PM

1. Enter the web version of okx Euyi Exchange ☜☜☜☜☜☜ Click to save 2. Click the link of okx Euyi Exchange app ☜☜☜☜ Click to save 3. After entering the official website, the clear interface provides a login and registration portal. Users can choose to log in to an existing account or register a new account according to their own situation. Whether it is viewing real-time market conditions, conducting transactions, or managing assets, the OKX web version provides a simple and smooth operating experience, suitable for beginners and veterans. Visit OKX official website now for easy experience

ok official portal web version ok exchange official web version login portal ok official portal web version ok exchange official web version login portal Mar 31, 2025 pm 06:24 PM

This article details how to use the official web version of OK exchange to log in. Users only need to search for "OK ​​Exchange Official Web Version" in their browser, click the login button in the upper right corner after entering the official website, and enter the user name and password to log in. Registered users can easily manage assets, conduct transactions, deposit and withdraw funds, etc. The official website interface is simple and easy to use, and provides complete customer service support to ensure that users have a smooth digital asset trading experience. What are you waiting for? Visit the official website of OK Exchange now to start your digital asset journey!

The latest registration tutorial for gate.io web version The latest registration tutorial for gate.io web version Mar 31, 2025 pm 11:15 PM

This article provides a detailed Gate.io web version latest registration tutorial to help users easily get started with digital asset trading. The tutorial covers every step from accessing the official website to completing registration, and emphasizes security settings after registration. The article also briefly introduces other trading platforms such as Binance, Ouyi and Sesame Open Door. It is recommended that users choose the right platform according to their own needs and pay attention to investment risks.

gate.io latest registration tutorial for beginners gate.io latest registration tutorial for beginners Mar 31, 2025 pm 11:12 PM

This article provides newbies with detailed Gate.io registration tutorials, guiding them to gradually complete the registration process, including accessing the official website, filling in information, identity verification, etc., and emphasizes the security settings after registration. In addition, the article also mentioned other exchanges such as Binance, Ouyi and Sesame Open Door. It is recommended that novices choose the right platform according to their own needs, and remind readers that digital asset investment is risky and should invest rationally.

gate official website login address gateio web version login portal address gate official website login address gateio web version login portal address Mar 31, 2025 pm 01:15 PM

Gate.io not only provides basic buying, selling and trading functions, but also launches a variety of innovative trading models and services to meet the needs of different users. The platform also provides a wealth of trading tools and analysis functions to help users make smarter investment decisions. Users can pledge their holdings to the platform, participate in mining activities, and obtain additional benefits.

See all articles