PHP browser version detect help

I am trying to get the Browser Version to output properly only outputs “?” any help would be greatly appreciated!

[php]<?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'    =>  'Mozilla Firefox',
                        '/safari/i'     =>  'Apple Safari',
                        '/chrome/i'     =>  'Google 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;

}
//####################################################
//############ PROBLEM AREA BELOW!!! #################
//######### RETURNS “?” NOT A VERSION NUMBER #########
//####################################################

function getVersion() {

global $user_agent;

$version        =   "Unknown Version";
$version_array  =   $known = array('Version', $version, 'other');
$pattern = '#(?<browser>' . join('|', $known) .
')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $user_agent, $matches)) {
    // we have no matching number just continue
}

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

    if (preg_match("/".$regex."/i", $user_agent)) {
        $version    =   $value;
    }

$i = count($matches[$browser]);
if ($i != 1) {
//we will have two since we are not using ‘other’ argument yet
//see if version is before or after the name
if (strripos($user_agent,“Version”) < strripos($user_agent,$version)){
$version= $matches[‘version’][0];
}
else {
$version= $matches[‘version’][1];
}
}
else {
$version= $matches[‘version’][0];
}

// check if we have a number
if ($version==null || $version=="") {$version="?";}

//0002089304102937

return $version;

} }

//####################################################
//############ PROBLEM AREA ABOVE!!! #################
//####################################################

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

$device_details = "Browser: ".$user_browser . " : " .$user_version .“
Operating System: “.$user_os.””;

print_r($device_details);
?>
[/php]

Output looks like:

Browser: Mozilla Firefox : ?
Operating System: Windows 7

This code doesn’t make a lot of sense. Did you get it from somewhere?

The getVersion() function is missing variables (e.g. $browser) and the $os_array loop is using regular array keys as $regex ??

it was 2 different scripts that I was messing around with the original script is
[php]<?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'    =>  'Mozilla Firefox',
                        '/safari/i'     =>  'Apple Safari',
                        '/chrome/i'     =>  'Google 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 = "Browser: ".$user_browser .“
Operating System: “.$user_os.””;

print_r($device_details);
?>[/php]

the whole script I took the version information from:
[php]<?php
function getBrowser()
{
$u_agent = $_SERVER[‘HTTP_USER_AGENT’];
$bname = ‘Unknown’;
$platform = ‘Unknown’;
$version= “”;

//First get the platform?
if (preg_match('/linux/i', $u_agent)) {
    $platform = 'Linux';
}
elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
    $platform = 'MAC';
}
elseif (preg_match('/windows|win32/i', $u_agent)) {
    $platform = 'Windows';
}

// Next get the name of the useragent yes seperately and for good reason
if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
{
    $bname = '<font color="FIREBRICK">Internet Explorer</font>';
    $ub = "MSIE";
}
elseif(preg_match('/Firefox/i',$u_agent))
{
    $bname = '<font color="DARKGREEN"><B><EM>Mozilla Firefox</B></EM></font>';
    $ub = "Firefox";
}
elseif(preg_match('/Chrome/i',$u_agent))
{
    $bname = '<font color="FIREBRICK">Google Chrome</font>';
    $ub = "Chrome";
}
elseif(preg_match('/Safari/i',$u_agent))
{
    $bname = '<font color="FIREBRICK">Apple Safari</font>';
    $ub = "Safari";
}
elseif(preg_match('/Opera/i',$u_agent))
{
    $bname = '<font color="FIREBRICK">Opera</font>';
    $ub = "Opera";
}
elseif(preg_match('/Netscape/i',$u_agent))
{
    $bname = '<font color="FIREBRICK">Netscape</font>';
    $ub = "Netscape";
}

// finally get the correct version number
$known = array('Version', $ub, 'other');
$pattern = '#(?<browser>' . join('|', $known) .
')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $u_agent, $matches)) {
    // we have no matching number just continue
}

// see how many we have
$i = count($matches['browser']);
if ($i != 1) {
    //we will have two since we are not using 'other' argument yet
    //see if version is before or after the name
    if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
        $version= $matches['version'][0];
    }
    else {
        $version= $matches['version'][1];
    }
}
else {
    $version= $matches['version'][0];
}

// check if we have a number
if ($version==null || $version=="") {$version="?";}

return array(

    'version'   => $version,
);

}

// now try it
$ua=getBrowser();
$yourbrowser= "Your Browser: " . $ua[‘name’] . " " . $ua[‘version’];
print_r($yourbrowser);
?>
[/php]

Then i was just throwing random variables in to try and make it work … but I got rid of all errors and ended with the “?” output … maybe you could help me using the FIRST script above and a “getversion” function to return the correct version number … like my mozilla should be “17.0” as per the second script … and not “?”

There is a very simple way to get this information if you have access to your php.ini file

http://php.net/manual/en/function.get-browser.php

You would need to download php_browscap.ini then set the path to it in your php.ini file
browscap = /path/to/php_browscap.ini

[php]
$browser = get_browser();
print_r($browser);
[/php]

Example output:

stdClass Object
(
    [browser_name_regex] => §^mozilla/5\.0 \(.*windows nt 6\.1.*wow64.*\).*gecko/.*firefox/16\..*$§
    [browser_name_pattern] => Mozilla/5.0 (*Windows NT 6.1*WOW64*)*Gecko/*Firefox/16.*
    [parent] => Firefox 16.0
    [platform] => Win7
    [platform_version] => 6.1
    [win32] => 
    [win64] => 1
    [comment] => Firefox 16.0
    [browser] => Firefox
    [version] => 16.0
    [majorver] => 16
    [minorver] => 0
    [beta] => 1
    [frames] => 1
    [iframes] => 1
    [tables] => 1
    [cookies] => 1
    [javascript] => 1
    [javaapplets] => 1
    [cssversion] => 3
    [alpha] => 
    [win16] => 
    [backgroundsounds] => 
    [vbscript] => 
    [activexcontrols] => 
    [ismobiledevice] => 
    [issyndicationreader] => 
    [crawler] => 
    [aolversion] => 0
)

To answer your question about this particular code…

If you want to merge your two functions together you have to change the getBrowser() function to use browser names recognized by getVersion(). For example, change these in browser_array:

[php]
‘/msie/i’ => ‘MSIE’,
‘/firefox/i’ => ‘Firefox’,
‘/safari/i’ => ‘Safari’,
‘/chrome/i’ => ‘Chrome’,
[/php]

Then, you would pass the return value of getBrowser() to getVersion(). For example:

[php]
function getVersion($ub) {
global $user_agent;

// finally get the correct version number
$known = array('Version', $ub, 'other');
$pattern = '#(?<browser>' . join('|', $known) .
')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $user_agent, $matches)) {
    // we have no matching number just continue
}

// see how many we have
$i = count($matches['browser']);
if ($i != 1) {
    //we will have two since we are not using 'other' argument yet
    //see if version is before or after the name
    if (strripos($user_agent,"Version") < strripos($user_agent,$ub)){
        $version= $matches['version'][0];
    }
    else {
        $version= $matches['version'][1];
    }
}
else {
    $version= $matches['version'][0];
}

// check if we have a number
if ($version==null || $version=="") {$version="?";}

return array(

    'version'   => $version,
);

}
[/php]

Usage:

[php]
$browser = getBrowser();
var_dump($browser);
$version = getVersion($browser);
var_dump($version);
[/php]

that seems to be working but
this is what my output looks like and i think it is because where Array is at i want the number and i have that defined as “$user_version” so i was wondering if theres anyway to take JUST the number 17.0 from the var_dump($version); ?

Browser: Firefox : Array <–says array instead of number
Operating System: Windows 7

string(7) “Firefox” array(1) { [“version”]=> string(4) “17.0” }

the output code:
[php]$user_os = getOS();
$user_browser = getBrowser();
$user_version = getVersion($browser);

//#######################################################
//######### Looking to put VERSION where $user_version is ##############
//#######################################################

$device_details = "Browser: “.$user_browser.” : ".$user_version .“
Operating System: “.$user_os.””;

print_r($device_details);

<? <? $browser = getBrowser(); var_dump($browser); $version = getVersion($browser); var_dump($version); ?>[/php]

[php]
return array(

    'version'   => $version,
);

[/php]

Replace with

[php]return $version;[/php]

after changing
[php] return array(

    'version'   => $version,
);[/php]

with
[php]return $version;[/php]
(rest of code the same)

Output:

Browser: Firefox : ? <–cannot seem to get this to change
Operating System: Windows 7

string(7) “Firefox” string(4) “17.0”

Because you are using an invalid variable on the first run

[php]
$user_browser = getBrowser();
$user_version = getVersion($browser);
[/php]

$browser does not exist

Duhhh :stuck_out_tongue: my bad … i was suppose to call $user_browser thanks for that!

Sponsor our Newsletter | Privacy Policy | Terms of Service