Catching guest user data with php

0

I'm setting up a support system and need to get some data from the client when it opens the service these data are: IP Operating System in this format (Windows 10 64x for ex) Browser used (Google Chrome Version 99)

The ip I can get normally. what is missing is the rest and the closest I got was this here;

$ip = $_SERVER['REMOTE_ADDR'];

$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = 'Unknown';
$platform = 'Unknown';
$version= "";

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';
}


if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
{
    $bname = 'Internet Explorer';
    $ub = "MSIE";
}
elseif(preg_match('/Firefox/i',$u_agent))
{
    $bname = 'Mozilla Firefox';
    $ub = "Firefox";
}
elseif(preg_match('/Chrome/i',$u_agent))
{
    $bname = 'Google Chrome';
    $ub = "Chrome";
}
elseif(preg_match('/AppleWebKit/i',$u_agent))
{
    $bname = 'AppleWebKit';
    $ub = "Opera";
}
elseif(preg_match('/Safari/i',$u_agent))
{
    $bname = 'Apple Safari';
    $ub = "Safari";
}

elseif(preg_match('/Netscape/i',$u_agent))
{
    $bname = 'Netscape';
    $ub = "Netscape";
}

$known = array('Version', $ub, 'other');
$pattern = '#(?<browser>' . join('|', $known) .')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $u_agent, $matches)) {
}


$i = count($matches['browser']);
if ($i != 1) {
    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="?";}

$Browser = array(
        'userAgent' => $u_agent,
        'name'      => $bname,
        'version'   => $version,
        'platform'  => $platform,
        'pattern'    => $pattern
);

$navegador = "Navegador: " . $Browser['name'] . " " . $Browser['version'];
$so = "SO: " . $Browser['platform'];

echo $navegador . "<br> $so";

and this returns

  

Browser: Google Chrome 60.0.3112.90   OS: Windows

So it's only necessary to bring in what system windows is in case if it is windows or what linux or other operating system it is and if available if it is 32 or 64x

Could anyone guide me with changing this function to display at least the OS version?

    
asked by anonymous 16.08.2017 / 18:28

1 answer

1

You can create an array with the OS and check the variable $user_agent - whose value is given by $_SERVER['HTTP_USER_AGENT'] - with foreach

$user_agent = $_SERVER['HTTP_USER_AGENT'];

function getOS() { 

   global $user_agent;

   $os_platform  =  "SO desconhecido";

   $os_array  =  array(
                        '/windows nt 10/i'     =>  'Windows 10',
                        '/windows nt 6.3/i'     =>  'Windows 8.1',
                        '/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;

}

$user_os = getOS();

echo "<strong>Sistema Operacional: </strong>".$user_os;

Font - Stack Exchange p>     

16.08.2017 / 23:34