Detect the code for an Android device through JavaScript or PHP

  • 2020-03-31 21:34:49
  • OfStack

With the return of Steve jobs and the release of the iPad2, the topic of mobile development seems to be heating up. Here are some of the ways to detect iOS's biggest competitor, Android.

JavaScript judgment method

Searching for Android words in the user agent string is the easiest way:
 
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 the STRSTR method in PHP to search for Android in the user agent:
 
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!
 
RewriteCond %{HTTP_USER_AGENT} ^.*Android.*$ 
RewriteRule ^(.*)$ http://android.davidwalsh.name [R=301] 

This gives you an idea of how to detect all three android devices.

The original link: http://article.yeeyan.org/view/56089/176760

Related articles: