Search code examples
phpmobiledevicedetect

Mobile devices detect


I have some page, but i need that users with randow user-agent (browsers or tablet, pc,..) see random info. I need to detect some device and do a manipulation with this info. For example, for iOS users i have one kind of info, for android another, and so on... Also for pc/laptop user includes one kind of css (even logos, events, scripts,...) for android another css, for blackberry another css and so on.

I think it's very important for absolutely responsive design.


Solution

  • So... I did it like this... Create a detect.php file and include it to a document. If someone have ideas about it.. please...

    <?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);
    
    ?>
    

    Now you can use it like:

    1. Include this file to all file that need to detect user device; you can include it once if you need For example: include_once ("path_to_file/detect.php");

    2. Use variable $DEVICE anywhere you need infinity times like:

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

    or

    !!! Also you can use it like this (if you need to separate info to different devices)

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

    That's it. All ingenious is simple! Use it and be happy! */ ?>