Home  >  Article  >  Backend Development  >  通过JavaScript或PHP检测Android设备的代码_PHP

通过JavaScript或PHP检测Android设备的代码_PHP

WBOY
WBOYOriginal
2016-06-01 12:17:07792browse

Android

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

JavaScript判断方法

搜索user agent字符串中的Android单词是最省事儿的方法:
复制代码 代码如下:
if(navigator.userAgent.match(/Android/i)) {
// Do something!
// Redirect to Android-site?
window.location = 'http://android.davidwalsh.name';
}

PHP判断方法

同样,我们可以在PHP中使用strstr方法搜索user agent中是否有Android:
复制代码 代码如下:
if(strstr($_SERVER['HTTP_USER_AGENT'],'Android')) {
header('Location: http://android.davidwalsh.name');
exit();
}

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

我们可以使用.htaccess来判断和响应安卓设备!
复制代码 代码如下:
RewriteCond %{HTTP_USER_AGENT} ^.*Android.*$
RewriteRule ^(.*)$ http://android.davidwalsh.name [R=301]

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

原文链接:http://article.yeeyan.org/view/56089/176760
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