Php: how to determine whether a user accesses through mobile wap or a computer _ PHP Tutorial

Source: Internet
Author: User
Tags website server
How does php determine whether a user accesses a mobile wap or a computer directly. Now we have a lot of smartphone users, and many friends visit the website using their mobile phones, this is a problem. if the access to a pc-based mobile phone is not good enough, it may be difficult to pay for the traffic. now we have a lot of smartphone users, and many friends visit the website directly using their mobile phones, this is a problem. if the mobile phone access in the pc version is difficult to see, the traffic is still charged, so many companies have made wap sites, but how can I determine whether a user accesses a website through a mobile phone or a pc? I have sorted out some methods below.

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 you can obtain the mobile phone number, it must be accessed through the mobile phone 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 mobile phone wap simulators that are popular on the internet-fundamentally.
The operation code is also simple:

The code is as follows:
// 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.

Iphone smartphone

The iphone version for discuz is basically completed. for convenient access, the iphone access is directly judged on the home page, and then directly redirected

The above code is used to determine that the test results are very good.

A judgment class used by myself, comprehensive

The code is as follows:

Determine whether the accessed user is a mobile phone bKjia. c0m
// Determine whether the mobile phone belongs
Function is_mobile (){
$ User_agent = strtolower ($ _ SERVER ['http _ USER_AGENT ']);
// Echo $ user_agent;
$ Mobile_agents = Array ("ipad", "wap", "android", "iphone", "sec", "sam", "ericsson", "240x320 ", "acer", "ACO on", "acs-", "abacho", "ahong", "airness", "alcatel", "amoi", " ", "applewebkit/525", "applewebkit/532", "asus", "audio", "au-mic", "avantogo", "becker", "benq ", "bilbo", "bird", "blackberry", "blazer", "bleu", "cdm-", "compal", "coolpad", "danger ", "dbtel", "dopod", "elaine", "eric", "etouch", "fly", "fly _", "fly-", "go. web "," goodaccess "," gradiente "," grundig "," haier "," hedy "," hitachi "," htc "," huawei "," Huchison ", "inno", "ipaq", "ipod", "jbrowser", "kddi", "kgt", "kwc", "lenovo", "lg", "lg2 ", "lg3", "lg4", "lg5", "lg7", "lg8", "lg9", "lg-", "lge-", "lge9 ", "longcos", "maemo", "mercator", "meridian", "micromax", "midp", "mini", "mitsu", "mmm", "mmp ", "mobi", "mot-", "moto", "nec-", "netfront", "newgen", "nexian", "nf-browser", "nintendo ", "nitro", "nokia", "nook", "novarra", "obigo", "palm", "panasonic", "pantech", "philips", "phone ", "pg-", "playstation", "pocket", "pt-", "qc-", "qtek", "rover", "sagem", "sama ", "samu", "sanyo", "samsung", "sch-", "scooter", "sec-", "sendo", "sgh-", "sharp ", "siemens", "sie-", "softbank", "sony", "spice", "sprint", "spv", "symbian", "tcl -", "teleca", "telit", "tianyu", "tim-", "toshiba", "tsm", "up. browser "," utec "," utstar "," verykool "," virgin "," vk-"," voda "," voxtel "," vx "," wellco ", "wig browser", "wii", "windows ce", "wireless", "xda", "xde", "zte", "ben", "hai ", "phili ");
$ Is_mobile = false;
Foreach ($ mobile_agents as $ device ){
If (stristr ($ user_agent, $ device )){
If ('iPad '= $ device)
Return $ is_mobile;
$ Is_mobile = true;
Return $ is_mobile;


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.