5
I need to get information from the operating system of who accessed the page, for example, through a command find out if it is Windows, MAC, Linux.
5
I need to get information from the operating system of who accessed the page, for example, through a command find out if it is Windows, MAC, Linux.
8
There are some methods, but no 100% guaranteed, as mentioned in Anderson’s answer.
The data relating to USER_AGENT
shall 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^^^^^^^^^^^^^
?>
Can extract only the SO
with a regex.
<?php
preg_match('((?<=\().*?(?=;))',$_SERVER['HTTP_USER_AGENT'],$matches);
echo $matches[0];
//Windows NT 6.1
?>
Or using the function get_browser():
<?php
$browser = get_browser();
echo $browser->platform;
//Win7
?>
As noted by @Andersoncarloswoss the function get_browser()
is dependent on
directive [browscap]
of php.ini
which by default is disabled, so 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.
8
First, there is no way to obtain this information with absolute certainty, as any and all client information passed to the server will be via HTTP request and therefore can be modified manually. That is, you can use such information, but you cannot trust it.
This type of information is passed to the server via HTTP request, more exactly through the header User-Agent
, which, in PHP, can be accessed as: $_SERVER["HTTP_USER_AGENT"]
. The value of this header will be something like:
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:
Mozilla
: 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 in general, this information is only present for historical reasons;
5.0
: Mozilla version;
Windows NT 10.0
: operating system information (this is the one that interests you, apparently);
Win64
: Indicates client uses Win32 API for Windows 64-bit;
x64
: 64-bit architecture of Windows;
AppleWebKit
: Webkit used;
537.36
: Used Build of Webkit;
KHTML
: Open source layout engine by the KDE project;
like Gecko
: Without conclusive information;
Chrome
: Name of the browser used;
59.0.3071.115
: Chrome version;
Safari
: Indicates that it is based on Safari;
537.36
: Build the Safari used;
OPR
: Without conclusive information;
46.0.2597.57
: Without conclusive information;
Example taken from http://www.useragentstring.com.
Much of this information you can obtain directly from the aforementioned website:
List of User Agent Strings: http://www.useragentstring.com/pages/useragentstring.php
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 don’t know if it would work in all cases (i.e. always the header follows the same pattern), so the easiest solutions are presented by Magichat and probably the libraries presented by William.
Interesting readings:
Here comes the show ;)!
3
Code posted in a post on the link: https://stackoverflow.com/questions/18070154/get-operating-system-info-with-php
Even returns 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']."");
?>
What is the need of global
?
@Guilhermenascimento I just posted the code of another post on the site in English. Any questions you can ask the author of the code by accessing the link provided in the reply.
It doesn’t mean that you can’t review/improve and until you improve ;)
3
As already stated in Anderson’s reply, it is not possible to detect in a guaranteed way, because the HTTP header called User-Agent
can be changed and so would cheat the script, creating a single regex is also not something easy and may not be so assertive (although maybe it is the best of ways).
The Magichat solution sometimes requires the browsercap.ini
or updates in the same that may not be easy to perform on production servers, that is to say you may be able to do this well on a local server or one that has administrator access, but for many servers this will not be possible.
To facilitate there are 3 libs that may be interesting as they do not require external services nor administrative access to the server, the only thing that is needed is the composer
for installation:
To install, in your project folder via terminal or cmd type:
composer require sinergi/browser-detector
Example of use:
<?php
require_once 'vendor/autoload.php';
use Sinergi\BrowserDetector\Os;
$os = new Os();
var_dump($os->getName());
In addition to detecting the browser and language
More details on: https://github.com/sinergi/php-browser-detector
It has cache system, detects Bots and a number of other options, to install in your project folder via terminal or cmd type:
composer require piwik/device-detector
Example of use:
<?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 https://github.com/piwik/device-detector
Agent is a lib as support for Laravel simple to use that also detects whether it is BOT, mobile or Desktop, to install in your project folder via terminal or cmd type:
composer require jenssegers/agent
Example of use:
<?php
require_once 'vendor/autoload.php';
use Jenssegers\Agent\Agent;
$agent = new Agent();
var_dump($agent->platform());
More details on https://github.com/jenssegers/agent
The code in the answer https://stackoverflow.com/a/18070424/1518921 (same source that David used in his reply) this one little "obsolete", and it is possible to make simple improvements, for example:
When you find the system you can use break
or return
within the foreach
thus leaving a little faster the answer (micro-optimization).
To decrease the code a little is to put the "delimiters" within the preg_match
so I wouldn’t have to write /..../i
for each item.
The use of global
just doesn’t make much sense, aside from leaving the variable $user_agent
in the scope "global" can cause accidents in the script.
The revised code would look 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;
Browser other questions tagged php operating-system
You are not signed in. Login or sign up in order to post.
From the computer operating system of the person accessing my website
– Maicon Wesley
@Uzumakiartanis It’s clear in the question "who accessed the page". :)
– Sam