Home  >  Article  >  Backend Development  >  PHP implements method to determine mobile phone device

PHP implements method to determine mobile phone device

墨辰丷
墨辰丷Original
2018-06-01 15:08:571646browse

This article mainly introduces the method of PHP to simply judge the mobile phone device, and involves the related skills of PHP to judge the operation based on the server's predefined variables. Friends in need can refer to it

Now the mobile Internet is becoming more and more popular , many websites have popularized mobile browsing. In order to better display web pages on mobile phones, we have chosen to use CSS media queries to create responsive templates, but this also has disadvantages. For example, the structure of some websites is CMS type. , there is too much content to display, and using CSS media queries to design responsive designs will only hide but still load. In order to display the content more quickly on the mobile phone, we can use this PHP to determine the mobile device code. Use this code to It is very convenient to display or not display customized content.

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;
}
?>
//这是调用代码,可以加上if判断:
<?php if(is_mobile()):?>
//设置手机端的内容
<?php endif; ?>

Summary: The above is the entire content of this article. I hope it can help everyone learn. Helps.

Related recommendations:

phpMethod to implement whether it is an ajax request

PHP AjaxForm Submit image upload and display image

php Detailed introduction of operators and expressions

The above is the detailed content of PHP implements method to determine mobile phone device. 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