代码之家  ›  专栏  ›  技术社区  ›  WebSon

移动设备检测

  •  -2
  • WebSon  · 技术社区  · 6 年前

    我有一些网页,但我需要与兰道用户代理(浏览器或平板电脑,pc,…)看到随机信息的用户。我需要检测一些设备并处理这些信息。例如,对于iOS用户,我有一种信息,对于android,我有另一种信息,等等。。。另外,对于pc/笔记本电脑用户还包括一种css(甚至是徽标、事件、脚本等),对于android另一种css,对于blackberry另一种css等等。

    我认为这是非常重要的绝对响应设计。

    1 回复  |  直到 6 年前
        1
  •  0
  •   WebSon    6 年前

    所以。。。我是这样做的。。。创建detect.php 归档并将其包含到文档中。如果有人对此有想法。。拜托。。。

    <?php
    
    $useragent = $_SERVER['HTTP_USER_AGENT']; //- Check user browser
    
    /* Do not change anything under this line */
    $iPod = stripos($useragent, "iPod"); 
    $iPad = stripos($useragent, "iPad"); 
    $iPhone = stripos($useragent, "iPhone");
    $Android = stripos($useragent, "Android"); 
    $iOS = stripos($useragent, "iOS"); 
    $webOS = stripos($useragent, "webOS"); 
    $Blackberry = stripos($useragent, "Blackberry");
    $IEMobile = stripos($useragent, "IEMobile"); 
    $OperaMini = stripos($useragent, "Opera mini"); 
    $Flynx = stripos($useragent, "Flynx"); 
    $Dolphin = stripos($useragent, "Dolphin");
    $Meego = stripos($useragent, "Meego"); 
    $Avantgo = stripos($useragent, "Avantgo"); 
    $Bada = stripos($useragent, "Bada"); 
    $Blazer = stripos($useragent, "Blazer");
    $Compal = stripos($useragent, "Compal"); 
    $Elaine = stripos($useragent, "Elaine"); 
    $Fennec = stripos($useragent, "Fennec"); 
    $Hiptop = stripos($useragent, "Hiptop");
    $Iris = stripos($useragent, "Iris"); 
    $Kindle = stripos($useragent, "Kindle"); 
    $Lge = stripos($useragent, "Lge "); 
    $Maemo = stripos($useragent, "Maemo");
    $Midp = stripos($useragent, "Midp"); 
    $Mmp = stripos($useragent, "Mmp"); 
    $Netfront = stripos($useragent, "Netfront"); 
    $OperaMobi = stripos($useragent, "Opera Mobi");
    $Palm = stripos($useragent, "Palm"); 
    $PalmOS = stripos($useragent, "Palm OS"); 
    $Phone = stripos($useragent, "Phone"); 
    $Plucker = stripos($useragent, "Plucker");
    $Pocket = stripos($useragent, "Pocket"); 
    $Psp = stripos($useragent, "Psp"); 
    $Symbian = stripos($useragent, "Symbian"); 
    $Treo = stripos($useragent, "Treo");
    $Vodafone = stripos($useragent, "Vodafone");
    $Wap = stripos($useragent, "Wap"); 
    $WindowsCe = stripos($useragent, "Windows Ce"); 
    $Xda = stripos($useragent, "Xda");
    $Xiino = stripos($useragent, "Xiino"); 
    $Kyivstar = stripos($useragent, "Kyivstar"); 
    $Bolt = stripos($useragent, "Bolt"); 
    $Boost = stripos($useragent, "Boost");
    $Cricket = stripos($useragent, "Cricket"); 
    $Docomo = stripos($useragent, "Docomo"); 
    $Fone = stripos($useragent, "Fone"); 
    $Mini = stripos($useragent, "Mini");
    $Mobi = stripos($useragent, "Mobi"); 
    $Pie = stripos($useragent, "Pie"); 
    $Tablet = stripos($useragent, "Tablet"); 
    $Wos = stripos($useragent, "Wos");
    $Kitkat = stripos($useragent, "Kitkat"); 
    $Mobile = stripos($useragent, "Mobile"); 
    $GoBrowser = stripos($useragent, "GoBrowser"); 
    $CLDC = stripos($useragent, "CLDC");
    $uZardWeb = stripos($useragent, "uZardWeb"); 
    $Doris = stripos($useragent, "Doris"); 
    $Skyfire = stripos($useragent, "Skyfire"); 
    $Smart = stripos($useragent, "Smart");
    $MobileSafari = stripos($useragent, "Mobile Safari");
    
    //-- Create a variable
    $DEVICE = ($iPod||$iPad||$iPhone||$Android||$iOS||$webOS||$Blackberry||$IEMobile||$OperaMini||$Dolphin||$Meego||$Avantgo||$Bada||$Blazer||$Compal||$Elaine||
    $Fennec||$Hiptop||$Iris||$Kindle||$Lge||$Maemo||$Midp||$Mmp||$Netfront||$OperaMobi||$Palm||$PalmOS||$Phone||$Plucker||$Pocket||$Psp||$Symbian||$Treo||$Wos||
    $Vodafone||$Wap||$WindowsCe||$Xda||$Xiino||$Kyivstar||$Flynx||$Bolt||$Boost||$Cricket||$Docomo||$Fone||$Mini||$Mobi||$Pie||$Tablet||$Kitkat||$Mobile||$Smart||
    $GoBrowser||$uZardWeb||$Doris||$Skyfire||$CLDC||$MobileSafari);
    
    ?>
    

    现在你可以像这样使用它:

    1. 将此文件包含到所有需要检测用户设备的文件中;如果你需要的话,你可以包括一次 例如:include \u once(“路径\u到\u文件/detect.php");

    2. 在任何需要无限次的地方使用变量$DEVICE,例如:

      <?php if (!$DEVICE) {echo 'This is for all desktop devices text';} else {echo 'This is for all mobile text';}
      ?>
      

    !!! 您也可以这样使用它(如果您需要将信息分离到不同的设备)

    <?php if ($DEVICE=$iPhone) {echo 'This is for iphone devices text';} else if ($DEVICE=$Blackberry) {echo 'This is for Blackberry text';}
    ?>
    

    就这样。一切巧妙都很简单!使用它并快乐!*/&燃气轮机;