PHP code to determine whether the device is a mobile phone or a tablet computer (two methods) _php instance

Source: Internet
Author: User

Now the mobile Internet is becoming more and more developed a lot of sites are popular mobile phone browsing, in order to better let the Web page on the mobile side display, we have chosen to use CSS Media Query production response template, but this also has drawbacks, such as the structure of some sites are CMS type, too much content to show, and the use of CSS Media query design response, only hidden but still loaded, in order to allow the phone to display the content faster, we can use this PHP to judge the phone device code, use this code can be very convenient to display or not display custom content.

When doing web development will often need to use the mobile device to match the page, of course, can be directly to make the site 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 way to use PHP to determine whether a device is a cell phone/tablet, from WordPress (wp-includes/vars.php:125), which is suitable for most types of cell 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 '), ' mobile ')!== 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 judge cell phone device function code, copied to the PHP function library call:

<?php
function 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 "," The 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 combined with if judgment:

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

Set the contents of the mobile phone end

<?php endif;?>

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

Related Article

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.