Ads

September 03, 2013

Android Detection with JavaScript or PHP

Today, I'm gonna tell you a new way of detecting Android Device, using PHP and Javascript. To Know more, please subscribe my blog, and you can follow me at twitter @nkondal1 Or at facebook facebook.com/nkondal1

 

The JavaScript:

Searching the user agent string for "Android" is the quickest method:
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
if(isAndroid) {
 // Do something!
 // Redirect to Android-site?
 window.location = 'http://android.site.name';
}
 
 

The PHP:

Again, we'll use PHP's strstr function to search for Android in the user agent:
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);  
if(stripos($ua,'android') !== false)  
{ 
// && stripos($ua,'mobile') !== false
{
header('Location: http://android.site.name'); 
exit(); 
}

Bonus!  .htaccess Detection:

We can even use .htaccess directives to detect and react to Android devices!
RewriteCond %{HTTP_USER_AGENT} ^.*Android.*$ RewriteRule ^(.*)$ http://android.davidwalsh.name [R=301]

No comments:

Most Popular Posts