Here is how to detect a mobile device using php.

Discussion in 'Web Design and Development' started by nerveosu, May 31, 2006.

  1. nerveosu macrumors member

    Joined:
    Sep 17, 2001
    #1
    For anyone who wants to use it..

    This is a way to detect with php if a visitor to your web site is using a mobile device browser.. it works. If you have a mobile device that this doesn't detect, all you have to do to add it is to find a unique bit of identifying information in your device's user agent and stick it in the list of mobile devices in this code.

    ------------

    <?php


    /* detect mobile device*/
    $ismobile = 0;
    $container = $_SERVER['HTTP_USER_AGENT'];

    // A list of mobile devices
    $useragents = array (

    'Blazer' ,
    'Palm' ,
    'Handspring' ,
    'Nokia' ,
    'Kyocera',
    'Samsung' ,
    'Motorola' ,
    'Smartphone',
    'Windows CE' ,
    'Blackberry' ,
    'WAP' ,
    'SonyEricsson',
    'PlayStation Portable',
    'LG',
    'MMP',
    'OPWV',
    'Symbian',
    'EPOC',

    );

    foreach ( $useragents as $useragents ) {
    if(strstr($container,$useragents)) {
    $ismobile = 1;
    }
    }



    if ( $ismobile == 1 ) {
    echo "<p>mobile device</p>";
    echo $_SERVER['HTTP_USER_AGENT'];
    }

    ?>
     
  2. beradrian macrumors newbie

    Joined:
    Oct 10, 2008
  3. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #3
  4. qwertydesign macrumors newbie

    Joined:
    Nov 18, 2008
    #4
    there is also a web service out there that provides GEO location as well as handset information real time.

    There are a few out there that do this, but we use handsetdetection.com when developing new sites for clients.
     
  5. web_god61 macrumors regular

    web_god61

    Joined:
    May 14, 2004
    #5
    wouldn't
    Code:
    in_array($_SERVER['HTTP_USER_AGENT'], $useragents)
    make more sense than a for loop?
     
  6. serbang macrumors newbie

    Joined:
    May 30, 2012
    #6
    re

    Even if this topic is a bit old, the mobile detection problem is rather new. Try Mobile_Detect class from http://code.google.com/p/php-mobile-detect/

    Code:
    Mobile_Detect is a lightweight PHP class for detecting mobile devices. It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.
     
  7. justindocanto macrumors newbie

    Joined:
    Jun 11, 2012
    #7
    I wrote an easy way to detect mobile browsers & devices in PHP

    I wrote this script to detect a mobile browser or device in PHP.

    The code detects a user based on the user-agent string by preg_match()ing. It has 100% accuracy on all current mobile devices and I'm currently updating it to support more mobile devices as they come out. The code is called isMobile and is as follows:

    Code:
    function isMobile() {
        return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
    }
    
    You can use it like this:

    Code:
    // Use the function
    if(isMobile())
        // Do something for only mobile users
    else
        // Do something for only desktop users
    
    To redirect a user to your mobile site, I would do this:

    Code:
    // Create the function, so you can use it
    function isMobile() {
        return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
    }
    
    // If the user is on a mobile device, redirect them
    if(isMobile())
        header("Location: http://m.yoursite.com/");
    
    Let me know if you have any questions and good luck!
     
  8. bpaluzzi macrumors 6502a

    bpaluzzi

    Joined:
    Sep 2, 2010
    Location:
    London
    #8
    Just FYI, you're missing some devices.
    e.g.:
    bada
    symbian
    ipad(!)
    ipod touch
    kindle
    ...


    Here's the version I use:
    http://detectmobilebrowsers.com/
     

Share This Page