Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Wednesday, September 19, 2012

Detect Android with PHP, Javascript & .htaccess

Android is a Linux-based operating system, designed primarily for touchscreen mobile devices such as smartphones and tablet computers. Andriod is initially developed by Android, Inc. 
While working in web technology environment, many times we came accross the situation where we need to detect that if it is Android is not. This detection can be done in multiple ways like: 
1. Using PHP 
2. Using Javascript 
3. Using .htaccess 
It totally depends on the interest of developer, which mechnaism he/she choose to detect android.

Detect Android Using PHP

Use  strstr() function to detect Android in the user agent: 

    $user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
    if(stripos($user_agent,'android') !== false) { 
      header('Location: http://www.google.com');
      exit();
    }
Detect Android Using Javascript
Use  indexOf() function to detect Android in the user agent:


    var user_agent = navigator.userAgent.toLowerCase();
    var isAndroidDevice = user_agent.indexOf("android") > -1; 
    if(isAndroidDevice) {
      // Write Code here..
     window.location = 'http://www.google.com';
   }
Detect Android Using .htaccess
We can use .htaccess directives to detect Android devices:

    RewriteCond %{HTTP_USER_AGENT} ^.*Android.*$
    RewriteRule ^(.*)$ http://www.google.com [R=301]

No comments:

Post a Comment