Home > Article > Backend Development > Detecting Android Devices via JavaScript or PHP_PHP Tutorial
With the return of Steve Jobs and the release of iPad2, 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
The easiest way to search for Android words in the user agent string:
<ol class="dp-c"> <li class="alt"><span><span class="keyword">if</span><span>(navigator.userAgent.match(/Android/i)) { </span></span></li> <li> <span> </span><span class="comment">// Do something! </span><span> </span> </li> <li class="alt"> <span> </span><span class="comment">// Redirect to Android-site? </span><span> </span> </li> <li> <span> window.location = </span><span class="string">'http://android.davidwalsh.name'</span><span>; </span> </li> <li class="alt"><span> } </span></li> </ol>
PHP Judgment Method
Similarly, we can use the strstr method in PHP to search whether There is Android:
<ol class="dp-c"> <li class="alt"><span><span class="keyword">if</span><span>(</span><span class="func">strstr</span><span>(</span><span class="vars">$_SERVER</span><span>[</span><span class="string">'HTTP_USER_AGENT'</span><span>],</span><span class="string">'Android'</span><span>)) { </span></span></li> <li> <span> header(</span><span class="string">'Location: http://android.davidwalsh.name'</span><span>); </span> </li> <li class="alt"> <span> </span><span class="func">exit</span><span>(); </span> </li> <li><span> } </span></li> </ol>
In addition, it can be determined by .htaccess
We can use .htaccess to determine and respond to Android devices!
<ol class="dp-xml"> <li class="alt"><span><span>RewriteCond %{HTTP_USER_AGENT} ^.*Android.*$ </span></span></li> <li> <span> RewriteRule ^(.*)$ http://android.davidwalsh.name [</span><span class="attribute">R</span><span>=</span><span class="attribute-value">301</span><span>] </span> </li> </ol>
In this way, you have mastered the detection methods of all three Android devices.
http://article.yeeyan.org/view/56089/176760