首页 >php教程 >php手册 >通过JavaScript或PHP检测Android设备

通过JavaScript或PHP检测Android设备

WBOY
WBOY原创
2016-06-06 19:36:151021浏览

随着乔布斯的回归,iPad2的发布,看来移动端的开发话题越来越火热了。在此列出一些能够在iOS的最大竞争者安卓(Android)系统的 检测 方法。 JavaScript判断方法 搜索user agent字符串中的Android单词是最省事儿的方法: 1 . if (navigator . userAgent . matc

  随着乔布斯的回归,iPad2的发布,看来移动端的开发话题越来越火热了。在此列出一些能够在iOS的最大竞争者——安卓(Android)系统的检测方法。

  JavaScript判断方法

  搜索user agent字符串中的Android单词是最省事儿的方法:

<p><span>1</span><span>.</span><span>if</span><span>(navigator</span><span>.</span><span>userAgent</span><span>.</span><span>match(</span><span>/</span><span>Android</span><span>/</span><span>i)) {  <br>   </span><span>2</span><span>.</span><span>//</span><span> Do something!  </span><span><br></span><span>3</span><span>.</span><span>//</span><span> Redirect to Android-site?  </span><span><br></span><span>4</span><span>.</span><span>    window</span><span>.</span><span>location </span><span>=</span><span>'</span><span>http://android.davidwalsh.name</span><span>'</span><span>;  <br>   </span><span>5</span><span>.</span><span>  } </span></p>

  PHP判断方法

  同样,我们可以在PHP中使用strstr方法搜索user agent中是否有Android:

<p><span>1</span><span>.</span><span>if</span><span>(</span><span>strstr</span><span>(</span><span>$_SERVER</span><span>[</span><span>'</span><span>HTTP_USER_AGENT</span><span>'</span><span>]</span><span>,</span><span>'</span><span>Android</span><span>'</span><span>)) {  <br>   </span><span>2</span><span>.</span><span>header</span><span>(</span><span>'</span><span>Location: http://android.davidwalsh.name</span><span>'</span><span>);  <br>   </span><span>3</span><span>.</span><span>exit</span><span>();  <br>   </span><span>4</span><span>.</span><span>  } </span></p>

  另外,可以通过.htaccess来判断

  我们可以使用.htaccess来判断和响应安卓设备!

<p><span>1</span><span>.</span><span> RewriteCond </span><span>%</span><span>{HTTP_USER_AGENT} </span><span>^.*</span><span>Android</span><span>.*</span><span>$  <br>   </span><span>2</span><span>.</span><span>  RewriteRule </span><span>^</span><span>(</span><span>.*</span><span>)$ http</span><span>:</span><span>//</span><span>android.davidwalsh.name [R=301] </span></p>

  这样你就掌握了全部三种安卓设备检测方法。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn