How do you know if you are currently visiting a mobile phone or a computer?

Source: Internet
Author: User
Tags readfile
How can I tell if I am currently accessing my phone or My computer?
Online now there are three views, one is based on the browser sent User-agent, but obviously this method is not feasible, although some people list 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 a new brand will be in the future.

Here's an example of PHP, and you'll see 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;
7. return false;

The other is to judge Http_accept, this should be relatively reliable, but http_accept is very complex, if the general HTML browser is good to say, you just have to judge the browser support WML and do not support HTML, But if the browser supports WML and HTML at the same time, the low-end phone may put WML in front of the HTML, but many high-end phones or smartphones have good HTML support, so HTML will appear in front of WML.

This is the http_accept sent by the BlackBerry phone.


Exaggerated, huh? But it is very standard, developers can get a lot of information based on this, but many phones are not so standard, if you look at IE or FF http_accept you will find it very short.


Also, 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 takes precedence over HTML], it is judged to be a phone. But in fact this principle is not correct.

There is a way to judge the gateway or IP or something, but I personally think this is very unreliable, the mobile phone is a lot of networking, such as when I developed a simulator. This should not be an example, REMOTE_ADDR is enough.

I found these on the Internet, do not know there is a way?

Study in the area.
Saying...... After determining the client type, what happens next?
Does the mobile browser support JavaScript?
Why don't you just go through the browser and judge what the system is?
1. Whether JS is supported,
2. Browser effective Area resolution
3. Time spent in a certain JS execution
4. User-agent

But you can't do it. 100% correct, these data can be forged
Learn ~ ~ ~
Let the operation of the people choose their own. It's certainly not easy to be mistaken.
Attention ....
  • Related Article

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