How does thinkphp determine whether a visitor is a mobile phone or a PC?

Source: Internet
Author: User
Recently, I am going to make a small upgrade for my website so that users can display templates suitable for mobile phones during QR code scanning [ThinkPHP3.0 is used]. the code is for reference by others, let me start with a rough idea of two simple steps: the system version number.

Recently, I am going to make a small upgrade for my website so that users can display templates suitable for display on the mobile phone terminal when scanning the QR code on their mobile phone [ThinkPHP3.0 is used]. the code is for reference by others.

Let me start with a rough idea of two simple steps:

The general version number, the browser version number, and the version number (only mobile terminal information is contained in the array in the pasted code, so you only need to judge whether it is a value in the array ).

Determine whether a visitor is a mobile phone, tablet, or other mobile terminal based on the values in the array, if yes, define the project path and NAME as your mobile phone template, for example, define ('app _ name', 'mobi ');

Define ('app _ path', './mobi /');

Add comments as much as possible for the following code:

  1. // Determine whether the mobile phone belongs
  2. // The code looks a lot. In fact, the array looks a lot messy and should not be intimidated by the surface phenomenon!
  3. Function is_mobile (){
  4. $ User_agent = $ _ SERVER ['http _ USER_AGENT '];
  5.  
  6. $ Mobile_agents = Array ("240x320", "acer", "ACO on", "acs-", "abacho", "ahong", "airness", "alcatel ", "amoi", "android", "anywhereyougo.com", "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", "ipad", "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 ", "tablet", "talkabout", "tcl-", "teleca", "telit", "tianyu", "tim-", "toshba", "tsm ", "up. browser "," utec "," utstar "," verykool "," virgin "," vk-"," voda "," voxtel "," vx "," wap ", "wellco", "wig browser", "wii", "windows ce", "wireless", "xda", "xde", "zte ");
  7. $ Is_mobile = false;
  8. Foreach ($ mobile_agents as $ device) {// Here, the value is traversed to find whether the above string has appeared
  9. If (stristr ($ user_agent, $ device) {// stristr to check whether the visitor information is in the preceding array. if the information does not exist, it is PC.
  10. $ Is_mobile = true;
  11. Break;
  12. }
  13. }
  14. Return $ is_mobile;
  15. }
  16. Define ('think _ path', './CORE /');
  17. If (is_mobile () {// jump to the wap Group
  18. Echo 'you have accessed the mobile phone and jumped to the mobile phone end ';
  19. Define ('app _ name', 'mobi ');
  20. Define ('app _ path', './mobi /');
  21. } Else {
  22. Echo 'You accessed from the PC ';
  23. Define ('app _ name', 'home ');
  24. Define ('app _ path', './Home /');
  25. }
  26. Define ('app _ debug', false );
  27. Require THINK_PATH. 'core. php ';

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.