Home  >  Article  >  Backend Development  >  PHP code to determine if the device is a phone or tablet

PHP code to determine if the device is a phone or tablet

不言
不言Original
2018-05-07 09:57:001454browse

This article mainly introduces the PHP code to determine whether the device is a mobile phone or a tablet. It has a certain reference value. Now I share it with you. Friends in need can refer to it

With the development of Internet mobile devices Popularity, many websites are compatible with mobile phone browsing. In order to better display web pages on mobile phones, we have chosen to use CSS media queries to create responsive templates. This article will introduce you to the PHP code to determine whether the device is a mobile phone or a tablet (two methods). Friends who are interested can learn together

Now the mobile Internet is becoming more and more developed, and many websites have popularized mobile browsing. In order to better display web pages on mobile phones, we all chose to use CSS media queries to create responsive templates, but this also has disadvantages. For example, the structure of some websites is CMS type, and there is too much content to display, and using CSS The media query design is responsive and will only be hidden but still loaded. In order to display the content more quickly on the mobile phone, we can use this PHP to determine the mobile device code. Using this code can easily display or not display customized content. .

When doing WEB development, you often need to use page matching for mobile devices. Of course, you can directly make the website responsive, but if you don’t want to do this, you can use PHP to determine the device type. , and then display the corresponding interface and content. Today I will share a method of using PHP to determine whether the device is a mobile phone/tablet. The method comes from WordPress (wp-includes/vars.php:125) and is suitable for most types of mobile phones/tablets:

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 function code to determine the mobile phone device. Copy it to the PHP function library and call it:

<?php
function is_mobile() {
$user_agent = $_SERVER[&#39;HTTP_USER_AGENT&#39;];
$mobile_browser = Array(
"mqqbrowser", //手机QQ浏览器
"opera mobi", //手机opera
"juc","iuc",//uc浏览器
"fennec","ios","applewebKit/420","applewebkit/525","applewebkit/532","ipad","iphone","ipaq","ipod",
"iemobile", "windows ce",//windows phone
"240×320","480×640","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. You can add if judgment :

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

Set the content on the mobile phone

<?php endif; ?>

The above is all of this article Content, hope you all like it.

Related recommendations:

PHP determines whether a file exists in the specified directory


##

The above is the detailed content of PHP code to determine if the device is a phone or tablet. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn