Home  >  Article  >  Backend Development  >  如何判断是手机浏览还是电脑浏览

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

WBOY
WBOYOriginal
2016-06-13 13:00:54988browse

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

判断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 />
}

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