HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Browser and OS detection script

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
anddetectionscriptbrowser

Problem

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?

  $browser,
            'os_platform'   =>  $os_platform
        );

} 

$user_agent     =   getBrowserOS();

$device_details =   "Browser: ".$user_agent['browser']."Operating System: ".$user_agent['os_platform']."";

print_r($device_details);

echo("".$_SERVER['HTTP_USER_AGENT']."");

?>


Update with new script

```
'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',

Solution

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.)

Code Snippets

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

// Override if matched    
    if (preg_match('/iphone/i', $user_agent)) {    
        $os_platform    =   "iPhone";
$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;    
    }    
}
if ($os_platform == "iPhone" || $os_platform == "Android" || $os_platform == "BlackBerry" 
    || $os_platform == "Mobile" || $os_platform == "iPod" || $os_platform == "iPad") {
$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)) {    
    ...    
}
$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;

Context

StackExchange Code Review Q#6077, answer score: 8

Revisions (0)

No revisions yet.