PHP code to determine whether the device is a mobile phone or tablet (two ways), PHP tablet _php tutorial

Source: Internet
Author: User

PHP code to determine whether the device is a mobile phone or tablet (two ways), PHP Tablet PC


Now the mobile Internet is more and more developed, a lot of websites have popularized the mobile browsing, in order to better let the Web page display on the phone, we have chosen to use CSS Media query to make the response template, but there are drawbacks, such as the structure of some sites are CMS type, too much content to display, and the use of CSS Media query design response, will only hide but still loaded, in order to let the mobile phone side more quickly display content, we can use this PHP to determine the phone device code, using this code can be easily displayed or do not display the custom content.

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:

Method One:

/** * Test If the current browser runs on a mobile device (smart phone, tablet, etc.) * * @staticvar bool $is _mobile * * @ return bool */function wp_is_mobile () {static $is _mobile = null;  if (Isset ($is _mobile)) {  return $is _mobile;}  if (Empty ($_server[' http_user_agent ')) {  $is _mobile = false;} elseif (Strpos ($_server[' http_user_agent '), ' Mobil E ')!== false//Many mobile devices (all IPhone, IPad, etc)  | | Strpos ($_server[' http_user_agent '), ' Android ')!== false  | | strpos ($_server[' http_user_agent '], ' silk/')!== False  | | strpos ($_server[' http_user_agent '), ' Kindle ')!== false  | | strpos ($_server[' http_user_agent '], ' BlackBerry ')!== false  | | strpos ($_server[' http_user_agent '), ' Opera Mini ')!== false  | | strpos ($_server[' HTTP _user_agent '], ' Opera Mobi ')!== false) {   $is _mobile = true;} else {  $is _mobile = false;}  return $is _mobile;}

Code two:

This is the PHP Judgment phone device function code, copied to the PHP function library called:

<?phpfunction Is_mobile () {$user _agent = $_server[' http_user_agent ']; $mobile _browser = Array ("Mqqbrowser",// Mobile QQ Browser "Opera mobi",//Mobile phone Opera "Juc", "Iuc",//uc Browser "Fennec", "ios", "applewebkit/420", "applewebkit/525", "AppleWebKit /532 "," ipad "," iphone "," iPAQ "," ipod "," Iemobile "," Windows CE ",//windows phone" 240x320 "," 480x640 "," Acer "," Android ", "Anywhereyougo.com", "Asus", "Audio", "BlackBerry", "Blazer", "Coolpad", "Dopod", "Etouch", "Hitachi", "HTC", "Huawei", " Jbrowser "," Lenovo "," LG "," lg-"," lge-"," Lge "," Mobi "," Moto "," Nokia "," Phone "," Samsung "," Sony "," Symbian "," tablet "," Tianyu "," WAP "," XDA "," Xde "," ZTE "); $is _mobile = False;foreach ($mobile _browser as $device) {if (Stristr ($user _agent, $ Device) {$is _mobile = True;break;}} return $is _mobile;}? >

This is the calling code, which can be added if judged:

<?php if (Is_mobile ()):?>

Set the content on the phone side

<?php endif;?>

The above is the whole content of this article, I hope you like it.

http://www.bkjia.com/PHPjc/1062503.html www.bkjia.com true http://www.bkjia.com/PHPjc/1062503.html techarticle PHP code to determine whether the device is a mobile phone or tablet (two methods), PHP tablet computer Now mobile Internet is more and more developed, a lot of sites are popular mobile browsing, in order to better let ...

  • 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.