PHP to determine whether a visitor is a PC or a mobile terminal

Source: Internet
Author: User

  1. function ismobile()
  2. { 
  3. //If there is http_x_wap_profile, it must be a mobile device .
  4. if (isset ($_server[' Http_x_wap_profile ']))
  5. {
  6. return true;
  7. } 
  8. //If via information contains WAP, it must be a mobile device, some service associations block this information
  9. if (isset ($_server[' Http_via ']))
  10. { 
  11. //cannot be found for flase, otherwise true
  12. return stristr($_server[' Http_via '], "WAP") ? true : false;
  13. } 
  14. //Brain residue method, determine the mobile phone to send the client logo, compatibility needs to be improved
  15. if (isset ($_server[' Http_user_agent ']))
  16. {
  17. $clientkeywords = array (' Nokia ',
  18. ' Sony ',
  19. ' Ericsson ',
  20. ' mot ',
  21. ' Samsung ',
  22. ' HTC ',
  23. ' SGH ',
  24. ' LG ',
  25. ' Sharp ',
  26. ' sie-',
  27. ' Philips ',
  28. ' Panasonic ',
  29. ' Alcatel ',
  30. ' Lenovo ',
  31. ' iphone ',
  32. ' ipod ',
  33. ' BlackBerry ',
  34. ' Meizu ',
  35. ' Android ',
  36. ' NetFront ',
  37. ' Symbian ',
  38. ' UCWeb ',
  39. ' WindowsCE ',
  40. ' Palm ',
  41. ' Operamini ',
  42. ' Operamobi ',
  43. ' Openwave ',
  44. ' Nexusone ',
  45. ' CLDC ',
  46. ' MIDP ',
  47. ' WAP ',
  48. ' Mobile '
  49. );  
  50. //Find the keywords in your phone browser from http_user_agent
  51. if (preg_match("/(") . Implode(' | ') , $clientkeywords) . ")/I", strtolower($_server[' http_user_agent '] ))
  52. {
  53. return true;
  54. } 
  55. } 
  56. //Protocol law, because there may be inaccuracies, put to the final Judgment
  57. if (isset ($_server[' http_accept ']))
  58. { 
  59. //If only WML is supported and HTML is not supported it must be a mobile device .
  60. //If WML and HTML are supported but WML is a mobile device before HTML
  61. If((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 '
  62. {
  63. return true;
  64. } 
  65. } 
  66. return false;
  67. } 
if ($this->ismobile ()) {//Jump to mobile page}else{//jump to PC-side page}

======================

First of all, a PHP lightweight identification class is recommended, Mobile-detect specifically identifies the mobile or PC-side access to the site, which can be based on the type of terminal access to the mobile browser or the Web site of the PC browser.

Mobile-detect website links are as follows Mobiledetect

The example links are as follows: Mobile-detect Example

Here is my simple to write the jump to the PC or mobile phone-side code:

[PHP]View PlainCopy
  1. <?php
  2. require_once ' mobile_detect.php '; //Note To introduce mobile_detect.php this class has a download link in the connection above
  3. $detect = new Mobile_detect;
  4. if ($detect->ismobile ()) {
  5. Header (' location:http://127.0.0.1/mobiledetect/mobiledetect/mobile.html ', true, 301);
  6. echo "mobile";
  7. }else{
  8. Header (' location:http://127.0.0.1/mobiledetect/mobiledetect/pc.html ', true, 301);
  9. echo "PC";
  10. }
  11. ?>

PHP to determine whether a visitor is a PC or a mobile terminal

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.