How to get information from the client's operating system?

4

I need to get information from the operating system of the person who accessed the page, for example through a command to find out if it is Windows, MAC, Linux.

    
asked by anonymous 07.08.2017 / 23:05

4 answers

9

There are some methods, but no 100% guaranteed, as mentioned in Anderson's response.

Data relative to USER_AGENT will be obtained through the environment variable HTTP_USER_AGENT :

<?php
echo $_SERVER['HTTP_USER_AGENT'];
//Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36
--------------^^^^^^^^^^^^^SO^^^^^^^^^^^^^
?>

You can only extract SO with a regex.

<?php
preg_match('((?<=\().*?(?=;))',$_SERVER['HTTP_USER_AGENT'],$matches);
echo $matches[0];
//Windows NT 6.1
?> 

Or using the get_browser () function:

<?php
$browser = get_browser();
echo $browser->platform;
//Win7
?>

As noted by @AndersonCarlosWoss the get_browser() function is dependent on the directive [browscap] of php.ini which by default is disabled, then it is necessary to enable it:

[browscap]
; http://php.net/browscap
browscap="\xampp\php\extras\php_browscap.ini"//este caminho é relativo para cada instalação

Where php_browscap.ini is the database maintained by browscap.org .

    
07.08.2017 / 23:26
8

First, there is no way to obtain this information with absolute certainty, since any client information passed to the server will be through the HTTP request and therefore can be modified manually. That is, you can use such information, but you can not trust it.

This type of information is passed to the server via HTTP request, more precisely through the User-Agent header, which in PHP can be accessed as: $_SERVER["HTTP_USER_AGENT"] . The value of this header will look something like this:

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36 OPR/46.0.2597.57

Where to read:

  • %: Indicates that the user agent is based on Mozilla, which is valid for Gecko browsers such as Firefox and Netscape. For other browsers, it indicates that it is compatible with Mozilla, but overall, this information is only present for historical reasons;

  • Mozilla : Mozilla version;

  • 5.0 : operating system information (this is the one that interests you, apparently);

  • Windows NT 10.0 : Windows 64-bit architecture;

  • Win64 : WebKit used;

  • x64 : WebKit build used;

  • AppleWebKit : Open source layout engine for the KDE project;

  • 537.36 : No conclusive information ;

  • KHTML : Name of the browser used;

  • like Gecko : Chrome version;

  • Chrome : Indicates that it is Safari based;

  • 59.0.3071.115 : Safari build used;

  • Safari : No conclusive information ;

  • 537.36 : No conclusive information ;

  

Example taken from link .

Many of this information you can get directly from the above site:

List of User Agent Strings : link

And even use the API provided by them:

<?php

$url = "http://www.useragentstring.com/?uas=%s&getJSON=all";
$url = sprintf($url, urlencode($_SERVER["HTTP_USER_AGENT"]));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$output = curl_exec($ch);

curl_close($ch);

$data = json_decode($output);

echo "Seu sistema operacional é:", $data->os_type;

Or using regular expressions to get the data, but I do not know if it would work in all cases (ie always the header follows the same pattern), so the easiest solutions are the one presented by MagicHat and probably the libraries presented by Guilherme .

Interesting readings:

07.08.2017 / 23:27
3

As already said in Anderson's response, it is not possible to detect in a guaranteed way, because the HTTP header called User-Agent can be altered and thus would fool the script, creating a single regex is not easy either and may not be so assertive (though it may be the best of the paths).

The MagicHat solution sometimes requires browsercap.ini or updates in it that may not be easy to perform on production servers, ie you might even be able to do this well on a local server or have administrator access , but for many servers this will not be possible.

To facilitate there are 3 libs that can be interesting, since they do not require external services or administrative access to the server, the only thing that is required is composer for installation:

Browser Detector

To install, in the folder of your project via terminal or cmd type:

composer require sinergi/browser-detector

Example usage:

<?php

require_once 'vendor/autoload.php';

use Sinergi\BrowserDetector\Os;

$os = new Os();

var_dump($os->getName());

In addition to detecting your browser and language

More details at: link

DeviceDetector

It has cache system, detects BOTs and a series of options, to install in the folder of your project via terminal or cmd type:

composer require piwik/device-detector

Example usage:

<?php

require_once 'vendor/autoload.php';

use DeviceDetector\DeviceDetector;

$dd = new DeviceDetector($_SERVER['HTTP_USER_AGENT']);

$dd->parse();

$osData = $dd->getOs();

var_dump($osData->name);

More details on link

Agent

Agent is a lib as support for Laravel simple to use that also detects if it is BOT, mobile or Desktop, to install in the folder of your project via terminal or cmd type:

composer require jenssegers/agent

Example usage:

<?php

require_once 'vendor/autoload.php';

use Jenssegers\Agent\Agent;

$agent = new Agent();

var_dump($agent->platform());

More details on link

With RegEx

The code in the answer link (the same font that David used in his answer) is a little " obsolete ", and it is possible to make simple improvements, for example:

  • When you find the system you can use break or return within foreach thus leaving the response a bit faster (micro-optimization).

  • The use of preg_match simply does not make much sense, otherwise leaving the /..../i variable in the "global" scope can cause crashes in the script.

    The revised code looks like this:

    <?php
    
    function getOS() {
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
    
        $os_array = array(
            'windows nt 10'      =>  'Windows 10',
            'windows nt 6\.3'     =>  'Windows 8.1',
            'windows nt 6\.2'     =>  'Windows 8',
            'windows nt 6\.1'     =>  'Windows 7',
            'windows nt 6\.0'     =>  'Windows Vista',
            'windows nt 5\.2'     =>  'Windows Server 2003/XP x64',
            'windows nt 5\.1'     =>  'Windows XP',
            'windows xp'         =>  'Windows XP',
            'windows nt 5\.0'     =>  'Windows 2000',
            'windows me'         =>  'Windows ME',
            'win98'              =>  'Windows 98',
            'win95'              =>  'Windows 95',
            'win16'              =>  'Windows 3.11',
            'macintosh|mac os x' =>  'Mac OS X',
            'mac_powerpc'        =>  'Mac OS 9',
            'linux'              =>  'Linux',
            'ubuntu'             =>  'Ubuntu',
            'iphone'             =>  'iPhone',
            'ipod'               =>  'iPod',
            'ipad'               =>  'iPad',
            'android'            =>  'Android',
            'blackberry'         =>  'BlackBerry',
            'webos'              =>  'Mobile'
        );
    
        foreach ($os_array as $regex => $value) {
            if (preg_match('/' . $regex . '/i', $user_agent)) {
                return $value;
            }
        }
    
        return 'Unknown OS Platform';
    }
    
    function getBrowser() {
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
    
        $browser_array = array(
            'msie'       =>  'Internet Explorer',
            'firefox'    =>  'Firefox',
            'safari'     =>  'Safari',
            'chrome'     =>  'Chrome',
            'edge'       =>  'Edge',
            'opera'      =>  'Opera',
            'netscape'   =>  'Netscape',
            'maxthon'    =>  'Maxthon',
            'konqueror'  =>  'Konqueror',
            'mobile'     =>  'Handheld Browser'
        );
    
        foreach ($browser_array as $regex => $value) {
            if (preg_match('/' . $regex . '/i', $user_agent)) {
                return $value;
            }
        }
    
        return 'Unknown Browser';
    }
    
    echo 'Sistema operacional: ', getOS(), PHP_EOL;
    echo 'Navegador: ', getBrowser(), PHP_EOL;
    echo 'User-Agent: ', $_SERVER['HTTP_USER_AGENT'], PHP_EOL;
    
        
  • 08.08.2017 / 05:03
    2

    Code posted to a post on the link: link

    Returns even the browser:

    <?php
    
    $user_agent     =   $_SERVER['HTTP_USER_AGENT'];
    
    function getOS() { 
    
        global $user_agent;
    
        $os_platform    =   "Unknown OS Platform";
    
        $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;
    
    }
    
    function getBrowser() {
    
        global $user_agent;
    
        $browser        =   "Unknown Browser";
    
        $browser_array  =   array(
                                '/msie/i'       =>  'Internet Explorer',
                                '/firefox/i'    =>  'Firefox',
                                '/safari/i'     =>  'Safari',
                                '/chrome/i'     =>  'Chrome',
                                '/edge/i'       =>  'Edge',
                                '/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']."");
    
    ?>
    
        
    07.08.2017 / 23:35