Detecting Android Devices via JavaScript or PHP_PHP Tutorial

WBOY
Release: 2016-07-22 09:02:27
Original
808 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

In this way, you have mastered the detection methods of all three Android devices.

http://article.yeeyan.org/view/56089/176760


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445839.htmlTechArticle 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 detect the Android system, the biggest competitor of iOS...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template