A PHP-judging device is a phone/tablet code

Source: Internet
Author: User
When doing web development often need to use the mobile device to match the page, of course, you can make the site directly responsive, but if you do not want to do so, you can use PHP to determine the type of device, and then display the corresponding interface and content. Today share a method of using PHP to determine whether a device is a phone/tablet, based on WordPress (wp-includes/vars.php:125), suitable for most types of phone/tablet judgments:
  1. /**
  2. * Test If the current browser runs on a mobile device (smart phone, tablet, etc.)
  3. *
  4. * @staticvar BOOL $is _mobile
  5. *
  6. * @return BOOL
  7. */
  8. function Wp_is_mobile () {
  9. static $is _mobile = null;
  10. if (Isset ($is _mobile)) {
  11. return $is _mobile;
  12. }
  13. if (Empty ($_server[' http_user_agent ')) {
  14. $is _mobile = false;
  15. } elseif (Strpos ($_server[' http_user_agent '), ' mobile ')!== false//Many Mobile devices (all IPhone, IPad, etc.)
  16. || Strpos ($_server[' http_user_agent '), ' Android ')!== false
  17. || Strpos ($_server[' http_user_agent '), ' silk/')!== false
  18. || Strpos ($_server[' http_user_agent '), ' Kindle ')!== false
  19. || Strpos ($_server[' http_user_agent '), ' BlackBerry ')!== false
  20. || Strpos ($_server[' http_user_agent '), ' Opera Mini ')!== false
  21. || Strpos ($_server[' http_user_agent '), ' Opera Mobi ')!== false) {
  22. $is _mobile = true;
  23. } else {
  24. $is _mobile = false;
  25. }
  26. return $is _mobile;
  27. }
Copy Code
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.