Take visitor user data with php

Asked

Viewed 1,268 times

0

I am setting up a support system and need to get some customer data when it opens the service these data are: IP Operating system in this format ( Windows 10 64x e.g.) Browser used ( Google Chrome Versao 99 )

ip I can pick up 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 just missing bring what windows system is in case if it is windows or which linux or other operating system it is and if available whether it is 32 or 64x

someone could guide me with changing this function to display at least the version of OS?

  • Related https://answall.com/questions/227228/como-pega-informa%C3%A7%C3%B5es-do-sistema-operacional-do-cliente/227235#227235

  • What exit would you like?

  • only thing missing is which version of windows by ex windows 10 or 8.1 or if it is mac or liux which version is by ex Centos 7 or Snow Leopard and etc

  • @Magichat is not duplicate?

1 answer

1


You can create an array with the OS and check on the variable $user_agent - whose value is given by $_SERVER['HTTP_USER_AGENT'] - with a 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;

Source - Stack Exchange

Browser other questions tagged

You are not signed in. Login or sign up in order to post.