PHP to determine whether the WAP phone client method of the detailed

Source: Internet
Author: User
    1. If you have http_x_wap_profile, it must be a mobile device.
    2. if (Isset ($_server[' http_x_wap_profile ')) {
    3. return true;
    4. }
Copy Code

Analysis: By getting the Http_x_wap_profile information returned by the client, it can be judged that it must be a mobile terminal, but not all mobile phones can return this information, this and wait for access via is the same, which involves terminal and service provider problems! Code:

    1. If the VIA message contains a WAP, it must be a mobile device, and some service providers block that information
    2. if (Isset ($_server[' Http_via '))
    3. {
    4. Not found for flase, otherwise true
    5. Return Stristr ($_server[' Http_via '), "WAP")? True:false;
    6. }
Copy Code

Analysis: This http_via contains a service provider to provide some personal information, if judged by this is the most accurate, but for example, mobile blocking this information. Therefore, only some of the service providers are valid. Code:

    1. Brain residue method, determine the mobile phone to send the client logo, compatibility needs to be improved
    2. if (Isset ($_server[' http_user_agent '))
    3. {
    4. $clientkeywords = Array (' Nokia ', ' Sony ', ' Ericsson ', ' mot ', ' Samsung ',
    5. ' HTC ', ' SGH ', ' lg ', ' sharp ', ' sie-', ' Philips ', ' Panasonic ', ' Alcatel ',
    6. ' Lenovo ', ' iphone ', ' ipod ', ' blackberry ', ' Meizu ', ' Android ', ' NetFront ',
    7. ' Symbian ', ' UCWeb ', ' windowsce ', ' palm ', ' operamini ', ' Operamobi ',
    8. ' Openwave ', ' nexusone ', ' cldc ', ' MIDP ', ' wap ', ' mobile ';
    9. Find keywords for your phone's browser from Http_user_agent
    10. if (Preg_match ("/(". Implode (' | '), $clientkeywords). ") /I ",
    11. Strtolower ($_server[' http_user_agent ')))
    12. {
    13. return true;
    14. }
    15. }
Copy Code

Analysis: This uses the client return information [Http_user_agent] To configure the phone terminal keyword. Imagine, to list all the mobile phone identification, can be accurately judged, is this feasible? So, as long as the main list of mobile phone brands, so it is basically OK. However, in the code there is a judgment UCWeb, this is not judged.

Because, I get the information and get the information of Firefox is consistent, also can't find UCWeb keyword. Therefore, the use of UCWeb browser can not be judged, waiting for new methods to appear. After all, UCWeb occupies the majority of users, but UCWeb is a PC browser standard, because some are mistaken for a PC browser can also! Code:

    1. Protocol law, because there may be inaccuracies, put to the final judgment
    2. if (Isset ($_server[' http_accept ')) {
    3. If only WML is supported and HTML is not supported it must be a mobile device.
    4. If WML and HTML are supported but WML is a mobile device before HTML
    5. if ((Strpos ($_server[' http_accept '), ' VND.WAP.WML ')!== false)
    6. && (Strpos ($_server[' http_accept '), ' text/html ') = = = False
    7. || (Strpos ($_server[' http_accept '), ' vnd.wap.wml ')
    8. < Strpos ($_server[' http_accept '), ' text/html ')))
    9. {
    10. return true;
    11. }
    12. }
Copy Code

Analysis: At the end of the deal, there is no agreement. Is the MIME type to judge, only to receive WML pages of the phone, needless to say must be a mobile terminal, but such naïve idea is impossible. Because, now mobile phone can gradually and the function of the computer close!? The type of page that is acceptable on the browser is also not just wml,html supported. Receive WML in front of HTML, this can be said to be a phone, but not absolute. For example, the blueberry is the HTML row before, WML in the back. So you can't make a judgment.

To summarize, write the following PHP Judgment page program.

  1. /**
  2. * Program: iswap.php to determine if it is accessed via mobile phone
  3. * Program returns: whether @return bool is a mobile device
  4. */
  5. function IsMobile ()
  6. {
  7. If you have http_x_wap_profile, it must be a mobile device.
  8. if (Isset ($_server[' http_x_wap_profile ')) {
  9. return true;
  10. }
  11. If the VIA message contains a WAP, it must be a mobile device, and some service providers block that information
  12. if (Isset ($_server[' Http_via '))
  13. {
  14. Not found for flase, otherwise true
  15. Return Stristr ($_server[' Http_via '), "WAP")? True:false;
  16. }
  17. Brain residue method, determine the mobile phone to send the client logo, compatibility needs to be improved
  18. if (Isset ($_server[' http_user_agent '))
  19. {
  20. $clientkeywords = Array (' Nokia ', ' Sony ', ' Ericsson ', ' mot ', ' Samsung ',
  21. ' HTC ', ' SGH ', ' lg ', ' sharp ', ' sie-', ' Philips ', ' Panasonic ', ' Alcatel ',
  22. ' Lenovo ', ' iphone ', ' ipod ', ' blackberry ', ' Meizu ', ' Android ', ' NetFront ',
  23. ' Symbian ', ' UCWeb ', ' windowsce ', ' palm ', ' operamini ', ' Operamobi ',
  24. ' Openwave ', ' nexusone ', ' cldc ', ' MIDP ', ' wap ', ' mobile ';
  25. Find keywords for your phone's browser from Http_user_agent
  26. if (Preg_match ("/(". Implode (' | '), $clientkeywords). ") /I ",
  27. Strtolower ($_server[' http_user_agent ')))
  28. {
  29. return true;
  30. }
  31. }
  32. Protocol law, because there may be inaccuracies, put to the final judgment
  33. if (Isset ($_server[' http_accept ')) {
  34. If only WML is supported and HTML is not supported it must be a mobile device.
  35. If WML and HTML are supported but WML is a mobile device before HTML
  36. if ((Strpos ($_server[' http_accept '), ' VND.WAP.WML ')!== false)
  37. && (Strpos ($_server[' http_accept '), ' text/html ') = = = False
  38. || (Strpos ($_server[' http_accept '), ' vnd.wap.wml ')
  39. < Strpos ($_server[' http_accept '), ' text/html ')))
  40. {
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. ?>
Copy Code
  • 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.