Home  >  Article  >  Backend Development  >  PHP vs. Javascript Detect code for Android devices via JavaScript or PHP

PHP vs. Javascript Detect code for Android devices via JavaScript or PHP

WBOY
WBOYOriginal
2016-07-29 08:44:351041browse

With the return of Steve Jobs and the release of iPad 2, it seems that the topic of mobile development is becoming more and more popular. Here are some detection methods that can be used on Android, the biggest competitor of iOS.
JavaScript Judgment Method
Searching for Android words in the user agent string is the easiest way:

Copy the code The code is as follows:


if(navigator.userAgent.match(/Android/i)) {
// Do something!
// Redirect to Android-site?
window.location = 'http://android.davidwalsh.name';
}


PHP judgment method
Similarly, we can use strstr in PHP Method to search if there is Android in the user agent:

Copy the code The code is as follows:


if(strstr($_SERVER['HTTP_USER_AGENT'],'Android')) {
header('Location: http:/ /android.davidwalsh.name');
exit();
}


In addition, it can be judged by .htaccess
We can use .htaccess to judge and respond to Android devices!

Copy the code The code is as follows :


RewriteCond %{HTTP_USER_AGENT} ^.*Android.*$
RewriteRule ^(.*)$ http://android.davidwalsh.name [R=301]


This way you have mastered all three Androids Equipment testing methods.
Original link: http://article.yeeyan.org/view/56089/176760

The above has introduced PHP and Javascript to detect the code of Android devices through JavaScript or PHP, including PHP and Javascript. I hope it will be helpful to friends who are interested in PHP tutorials.

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