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

通过JavaScript或PHP检测Android设备

WBOY
Release: 2016-06-06 19:36:15
Original
1031 people have browsed it

随着乔布斯的回归,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>
Copy after login

  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>
Copy after login

  另外,可以通过.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>
Copy after login

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

Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template