使用JavaScript或PHP进行Android检测

    技术2022-07-11  143

    I've noticed that two of my blog posts continue to get more popular each week:  iPad Detection with JavaScript or PHP and iPhone and iPad detection with JavaScript or PHP. What's obvious is that Android development is a hot topic that will only grow.  Here are a few methods by which you can detect iOS' main competitor:  Android.

    我注意到,我的两个博客文章继续每周越来越受欢迎: 使用JavaScript或PHP和iPhone进行iPad检测以及使用JavaScript或PHP进行iPad检测 。 显而易见,Android开发是一个热门话题,并且只会不断增长。 您可以通过以下几种方法来检测iOS的主要竞争对手:Android。

    JavaScript (The JavaScript)

    Searching the user agent string for "Android" is the quickest method:

    在用户代理字符串中搜索“ Android”是最快的方法:

    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.davidwalsh.name'; }

    PHP (The PHP)

    Again, we'll use PHP's strstr function to search for Android in the user agent:

    同样,我们将使用PHP的strstr函数在用户代理中搜索Android:

    $ua = strtolower($_SERVER['HTTP_USER_AGENT']); if(stripos($ua,'android') !== false) { // && stripos($ua,'mobile') !== false) { header('Location: http://android.davidwalsh.name'); exit(); }

    奖金! .htaccess检测 (Bonus!  .htaccess Detection)

    We can even use .htaccess directives to detect and react to Android devices!

    我们甚至可以使用.htaccess指令来检测Android设备并对其做出React!

    RewriteCond %{HTTP_USER_AGENT} ^.*Android.*$ RewriteRule ^(.*)$ http://android.davidwalsh.name [R=301]

    And there you have it:  three different Android device detection!  Have fun with your mobile development!

    一切就绪:三种不同的Android设备检测! 享受您的移动开发乐趣!

    翻译自: https://davidwalsh.name/detect-android

    相关资源:通过JavaScript或PHP检测Android设备的代码
    Processed: 0.016, SQL: 9