Home Backend Development PHP Tutorial 如何判断是手机浏览还是电脑浏览

如何判断是手机浏览还是电脑浏览

Jun 13, 2016 pm 01:00 PM
http nbsp return server

怎么判断是手机浏览还是电脑浏览
想根据设备的不同而显示不同的页面

判断IP吗???那二者IP段分别是什么~~~

还是判断浏览器类型???这个貌似浏览器太多~~~
------解决方案--------------------
没有绝对的方法能分别。
提供以下函数,如果不是刻意混淆,成功在99%以上~

function isMobile() {<br />
  if(isset($_SERVER['HTTP_X_WAP_PROFILE'])){<br />
    return TRUE;<br />
  }<br />
<br />
  if(isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap")){<br />
    return TRUE;<br />
  }<br />
<br />
  if(isset($_SERVER['HTTP_USER_AGENT'])){<br />
    $clientkeywords = array('nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp', 'sie-',<br />
      'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu', 'android', 'netfront', 'symbian',<br />
      'ucweb', 'windowsce', 'palm', 'operamini', 'operamobi', 'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile');<br />
    if(preg_match("/(" . implode('<br><font color='#FF8000'>------解决方案--------------------</font><br>', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))){<br />
      return TRUE;<br />
    }<br />
  }<br />
<br />
  if(isset($_SERVER['HTTP_ACCEPT'])){<br />
    if((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false)<br />
          && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false<br />
              <br><font color='#FF8000'>------解决方案--------------------</font><br> (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))){<br />
      return TRUE;<br />
    }<br />
  }<br />
<br />
  return FALSE;<br />
}
Copy after login

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

Solution: Your organization requires you to change your PIN

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

10 Ways to Adjust Brightness on Windows 11

How to turn off private browsing authentication for iPhone in Safari? How to turn off private browsing authentication for iPhone in Safari? Nov 29, 2023 pm 11:21 PM

How to turn off private browsing authentication for iPhone in Safari?

What does http status code 520 mean? What does http status code 520 mean? Oct 13, 2023 pm 03:11 PM

What does http status code 520 mean?

Win10/11 digital activation script MAS version 2.2 re-supports digital activation Win10/11 digital activation script MAS version 2.2 re-supports digital activation Oct 16, 2023 am 08:13 AM

Win10/11 digital activation script MAS version 2.2 re-supports digital activation

Detailed explanation of the usage of return in C language Detailed explanation of the usage of return in C language Oct 07, 2023 am 10:58 AM

Detailed explanation of the usage of return in C language

How to install, uninstall, and reset Windows server backup How to install, uninstall, and reset Windows server backup Mar 06, 2024 am 10:37 AM

How to install, uninstall, and reset Windows server backup

Understand common application scenarios of web page redirection and understand the HTTP 301 status code Understand common application scenarios of web page redirection and understand the HTTP 301 status code Feb 18, 2024 pm 08:41 PM

Understand common application scenarios of web page redirection and understand the HTTP 301 status code

See all articles