Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I understand not to rely on user agent information for anything detrimental towards the site since it can be faked or hidden etc, it's more of just an extra feature for something.

Is there anyway this can be made shorter perhaps? Also it will be running on a few pages, so I was wanting to know if it's performance is good/bad?

<?php

function getBrowserOS() { 

    $user_agent     =   $_SERVER['HTTP_USER_AGENT']; 
    $browser        =   "Unknown Browser";
    $os_platform    =   "Unknown OS Platform";

    // Get the Operating System Platform

        if (preg_match('/windows|win32/i', $user_agent)) {

            $os_platform    =   'Windows';

            if (preg_match('/windows nt 6.2/i', $user_agent)) {

                $os_platform    .=  " 8";

            } else if (preg_match('/windows nt 6.1/i', $user_agent)) {

                $os_platform    .=  " 7";

            } else if (preg_match('/windows nt 6.0/i', $user_agent)) {

                $os_platform    .=  " Vista";

            } else if (preg_match('/windows nt 5.2/i', $user_agent)) {

                $os_platform    .=  " Server 2003/XP x64";

            } else if (preg_match('/windows nt 5.1/i', $user_agent) || preg_match('/windows xp/i', $user_agent)) {

                $os_platform    .=  " XP";

            } else if (preg_match('/windows nt 5.0/i', $user_agent)) {

                $os_platform    .=  " 2000";

            } else if (preg_match('/windows me/i', $user_agent)) {

                $os_platform    .=  " ME";

            } else if (preg_match('/win98/i', $user_agent)) {

                $os_platform    .=  " 98";

            } else if (preg_match('/win95/i', $user_agent)) {

                $os_platform    .=  " 95";

            } else if (preg_match('/win16/i', $user_agent)) {

                $os_platform    .=  " 3.11";

            }

        } else if (preg_match('/macintosh|mac os x/i', $user_agent)) {

            $os_platform    =   'Mac';

            if (preg_match('/macintosh/i', $user_agent)) {

                $os_platform    .=  " OS X";

            } else if (preg_match('/mac_powerpc/i', $user_agent)) {

                $os_platform    .=  " OS 9";

            }

        } else if (preg_match('/linux/i', $user_agent)) {

            $os_platform    =   "Linux";

        }

        // Override if matched

            if (preg_match('/iphone/i', $user_agent)) {

                $os_platform    =   "iPhone";

            } else if (preg_match('/android/i', $user_agent)) {

                $os_platform    =   "Android";

            } else if (preg_match('/blackberry/i', $user_agent)) {

                $os_platform    =   "BlackBerry";

            } else if (preg_match('/webos/i', $user_agent)) {

                $os_platform    =   "Mobile";

            } else if (preg_match('/ipod/i', $user_agent)) {

                $os_platform    =   "iPod";

            } else if (preg_match('/ipad/i', $user_agent)) {

                $os_platform    =   "iPad";

            }

    // Get the Browser

        if (preg_match('/msie/i', $user_agent) && !preg_match('/opera/i', $user_agent)) { 

            $browser        =   "Internet Explorer"; 

        } else if (preg_match('/firefox/i', $user_agent)) { 

            $browser        =   "Firefox";

        } else if (preg_match('/chrome/i', $user_agent)) { 

            $browser        =   "Chrome";

        } else if (preg_match('/safari/i', $user_agent)) { 

            $browser        =   "Safari";

        } else if (preg_match('/opera/i', $user_agent)) { 

            $browser        =   "Opera";

        } else if (preg_match('/netscape/i', $user_agent)) { 

            $browser        =   "Netscape"; 

        } 

        // Override if matched

            if ($os_platform == "iPhone" || $os_platform == "Android" || $os_platform == "BlackBerry" || $os_platform == "Mobile" || $os_platform == "iPod" || $os_platform == "iPad") { 

                if (preg_match('/mobile/i', $user_agent)) {

                    $browser    =   "Handheld Browser";

                }

            }

    // Create a Data Array

        return array(
            'browser'       =>  $browser,
            'os_platform'   =>  $os_platform
        );

} 


$user_agent     =   getBrowserOS();

$device_details =   "<strong>Browser: </strong>".$user_agent['browser']."<br /><strong>Operating System: </strong>".$user_agent['os_platform']."";

print_r($device_details);

echo("<br /><br /><br />".$_SERVER['HTTP_USER_AGENT']."");

?>

Update with new script

<?php

$user_agent     =   $_SERVER['HTTP_USER_AGENT'];

function getOS() { 

    global $user_agent;

    $os_platform    =   "Unknown OS Platform";

    $os_array       =   array(
                            '/windows nt 6.2/i'     =>  'Windows 8',
                            '/windows nt 6.1/i'     =>  'Windows 7',
                            '/windows nt 6.0/i'     =>  'Windows Vista',
                            '/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
                            '/windows nt 5.1/i'     =>  'Windows XP',
                            '/windows xp/i'         =>  'Windows XP',
                            '/windows nt 5.0/i'     =>  'Windows 2000',
                            '/windows me/i'         =>  'Windows ME',
                            '/win98/i'              =>  'Windows 98',
                            '/win95/i'              =>  'Windows 95',
                            '/win16/i'              =>  'Windows 3.11',
                            '/macintosh|mac os x/i' =>  'Mac OS X',
                            '/mac_powerpc/i'        =>  'Mac OS 9',
                            '/linux/i'              =>  'Linux',
                            '/ubuntu/i'             =>  'Ubuntu',
                            '/iphone/i'             =>  'iPhone',
                            '/ipod/i'               =>  'iPod',
                            '/ipad/i'               =>  'iPad',
                            '/android/i'            =>  'Android',
                            '/blackberry/i'         =>  'BlackBerry',
                            '/webos/i'              =>  'Mobile'
                        );

    foreach ($os_array as $regex => $value) { 

        if (preg_match($regex, $user_agent)) {
            $os_platform    =   $value;
        }

    }   

    return $os_platform;

}

function getBrowser() {

    global $user_agent;

    $browser        =   "Unknown Browser";

    $browser_array  =   array(
                            '/msie/i'       =>  'Internet Explorer',
                            '/firefox/i'    =>  'Firefox',
                            '/safari/i'     =>  'Safari',
                            '/chrome/i'     =>  'Chrome',
                            '/opera/i'      =>  'Opera',
                            '/netscape/i'   =>  'Netscape',
                            '/maxthon/i'    =>  'Maxthon',
                            '/konqueror/i'  =>  'Konqueror',
                            '/mobile/i'     =>  'Handheld Browser'
                        );

    foreach ($browser_array as $regex => $value) { 

        if (preg_match($regex, $user_agent)) {
            $browser    =   $value;
        }

    }

    return $browser;

}


$user_os        =   getOS();
$user_browser   =   getBrowser();

$device_details =   "<strong>Browser: </strong>".$user_browser."<br /><strong>Operating System: </strong>".$user_os."";

print_r($device_details);

echo("<br /><br /><br />".$_SERVER['HTTP_USER_AGENT']."");

?>

Added a couple more browsers and operating systems to the list in the new version :)

share|improve this question
    
When the if matches I would not $browser = $value but return $value instead. –  konijn Feb 13 '13 at 18:21
add comment

1 Answer

up vote 7 down vote accepted

First of all, I guess somebody has already written a library for that. I would do some research and check existing libraries.

1, Split the code to two smaller functions: getOperatingSystem() and getBrowser().

2,

} else if (preg_match('/linux/i', $user_agent)) {    
    $os_platform    =   "Linux";    
}    

// Override if matched    
    if (preg_match('/iphone/i', $user_agent)) {    
        $os_platform    =   "iPhone";    

The second if should be on the same indentation level as the else if. It's a little bit confusing.

3, I'd put the regular expressions and the result browsers to an associative array and iterate over it:

$os_arr['/windows|win32/i'] = 'Windows';    
$os_arr['/windows nt 6.2/i'] = 'Windows 8';    
...    

foreach ($os_arr as $regexp => $value) {    
    if (preg_match($regexp, $user_agent)) {    
        $os_platform = $value;    
    }    
}    

It isn't exactly the same logic as your if-elseif structure but it also could work and it's more simple. Note: the order of the elements in the array is important.

4, Instead of this:

if ($os_platform == "iPhone" || $os_platform == "Android" || $os_platform == "BlackBerry" 
    || $os_platform == "Mobile" || $os_platform == "iPod" || $os_platform == "iPad") {     

set an $is_mobile flag:

$is_mobile = false;    
if (preg_match('/iphone/i', $user_agent)) {    
    $os_platform    =   "iPhone";    
    $is_mobile = true;    
} else if (preg_match('/android/i', $user_agent)) {    
        $os_platform    =   "Android";    
    $is_mobile = true;    
...    


if ($is_mobile && preg_match('/mobile/i', $user_agent)) {    
    ...    
}  

You can also combine it with the associative array solution:

$os_arr['/windows|win32/i']['os'] = 'Windows';    
$os_arr['/windows|win32/i']['is_mobile'] = FALSE;    
$os_arr['/windows nt 6.2/i']['os'] = 'Windows 8';    
$os_arr['/windows nt 6.2/i']['is_mobile'] = FALSE;  

(If you use it you should change the os and is_mobile strings to constants.)

share|improve this answer
1  
Nice! Definitely liking those changes, makes a lot more sense! Thank you palacsint! –  Joe Nov 16 '11 at 13:47
1  
Updated the question with the new code if anyone wants the improved version, thanks again palacsint, feels a lot better with the new code! –  Joe Nov 17 '11 at 10:54
    
There's actually a built-in function: get_browser() php.net/manual/en/function.get-browser.php , though it relies on a PHP_INI_SYSTEM config variable to so you need to be able to edit php.ini or httpd.conf to enable it. –  therefromhere Jul 5 '12 at 1:47
1  
Thanks a lot :) Simple, clean and up to date. –  user22104 Feb 13 '13 at 17:19
    
@user22104 It hasn't been updated for a few months now so a few of the new OS/Browsers might lead to 'Unknown Browser/OS' results. Things such as Windows 8, iOS6 etc have all been released since I updated this, if I get some spare time later I'll update it as best as I can :) –  Joe Feb 13 '13 at 20:25
add comment

protected by Jamal Dec 4 '13 at 7:58

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.