How can I determine whether the current access is from a mobile phone or a computer?-php Tutorial

Source: Internet
Author: User
Tags php example
How can I determine whether a mobile phone is used for access or a computer? 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 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 difficult it is to determine whether the current access is from a mobile phone or a computer?
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 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.



I found this on the internet. do you know how to use it/

------ Solution --------------------
Land occupation learning.
Saying ...... After determining the client type, what will happen next?
Does the mobile browser support javascript?
------ Solution --------------------
Follow
------ Solution --------------------
It's better to determine what the system is by using a browser.
------ Solution --------------------
1. is js supported,
2. valid browser region resolution
3. time spent on execution of a js segment
4. user-agent

However, these data cannot be 100% correct and can be forged.
------ Solution --------------------
Learning ~~~
------ Solution --------------------
Learning --
------ Solution --------------------
Learning ---
------ Solution --------------------
I learned ..
------ Solution --------------------
Let the operator select it. it is certainly not easy to make a mistake.
------ Solution --------------------
.......

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.