Mobile UA (User Agent) Detection Function in PHP

arp14 PHP, Tutorial Tags: , , , , 0

This is a simple PHP function to detect Mobile UA (User Agent). You can uncomment line 24 and line 28 to get which user agents got caught by this function. Returned values are ‘mobile’ if UA is mobile and ‘computer’ if not mobile.

Mobile UA (User Agent) Detection Function in PHP

        Mobile UA (User Agent) Detection Function in PHP 
        By Arpan Das
    echo $_SERVER['HTTP_USER_AGENT'].'<hr />'; // just see current user agent

    function mob_ua_detect () {
        $ua = strtolower($_SERVER['HTTP_USER_AGENT']);

        $agents = array('newt', 'uc browser', 'proxinet', 'mmp', 'playstation portable', 'danger',
                    'docomo', 'up.browser', 'windows mobile', 'maemo', 's60', 'eudoraweb', 'blackberry',
                    'series60', 'windowsmobile', '', 'blazer', 'symbian', 'mmef20', 'wap', 'palm',
                    'opera mini', 'motorola', 'handspring', 'ts21i-10', 'sonyericsson', 'portalmmm', 
                    'android', 'elaine/3.0', 'mobile', 'lg', 'cellphone', 'mot-v', 'avantgo', 'micromax',
                    'bada', 'opwv', 'hiptop', 'kyocera', 'netfront', 'palm', 'mot', 'sharp-tq-gx10', 
                    'ipod', 'smartphone', 'symbianos', 'iphone', 'windows ce', 'ucweb', 'samsung', 'epoc',
                    'small', 'nokia', 'palmos', 'xolo', 'pantech', 'xperia', 'htc', 'zte', 'karbonn',
                    't-mobile', 'plum', 'parla', 'huawei', 'alcatel', 'vodafone', 'blu', 'spice', 'prestigio',
                    'icemobile', 'verykool', 'vertu', 'celkon', 'niu', 'yezz');

        foreach ($agents as $agent) {
            if (strpos($ua, $agent) !== false) {
                //echo '<b style="color:orangered">Its ['.$agent.']</b><br />';
                $detected = 'mobile';
            } else {
                //echo '<span style="color:grey">Not ['.$agent.']</span><br />';
                $detected = 'computer';
        return $detected;

    echo mob_ua_detect();

This Mobile UA detection function works fine. But on a serious production machine, I recommended you to use Mobile Detect PHP class. It is a lightweight PHP class for detecting mobile devices (including tablets). You can get if from

You can see a demo here ->

You need to download and extract it, then just include and instantiate the class. The isMobile() method returns Boolean, true or false. If mobile detected, it will be true, else it will be false. You can see an example in codes below.

// including the mobile detect ua detection class

$detect_UA = new Mobile_Detect();
$isMobile = $detect_UA->isMobile();

if ( $isMobile ) echo 'mobile';
else echo 'computer';

Need help on Mobile UA Detection? Just leave a comment, I’ll be back to you.

Thank you!

Leave a Reply

Note: Your email address will not be published. Required fields are marked *