I recently started wondering about sites like http://thismachine.info/ that get the user's operating system info. I have not been able to find out how to do that with PHP, and wanted to try to figure it out.
I noticed that they list the user-agent
, which gives lots of info about the browser. Do they get the operating system information from that, or from something else? Is there an API I could use to get the user's operating system?
I see how they got the Browser and IP, but could not figure out the Operating System part!
Took the following code from php manual for get_browser.
The
$browser
array hasplatform
information included which gives you the specific Operating System in use.Please make sure to see the "Notes" section in that page. This might be something (thismachine.info) is using if not something already pointed in other answers.
Based on the answer by Fred-II I wanted to share my take on the getOS function, it avoids globals, merges both lists and detects the architecture (x32/x64)
If you want to get all those information, you might want to read this:
http://php.net/manual/en/function.get-browser.php
You can run the sample code and you'll see how it works:
The above example will output something similar to:
You can look for this information in
$_SERVER['HTTP_USER_AGENT']
, but its format is free-form, not guaranteed to be sent, and could easily be altered by the user, whether for privacy or other reasons.If you've not set the
browsecap
directive, this will return a warning. To make sure it's set, you can retrieve the value usingini_get
and see if it's set.As kba explained in his answer, your browser sends a lot of information to the server while loading a webpage. Most websites use these User-agent information to determine the visitor's operating system, browser and various information.
When you go to a website, your browser sends a request to the web server including a lot of information. This information might look something like this:
These information are all used by the web server to determine how to handle the request; the preferred language and whether compression is allowed.
In PHP, all this information is stored in the
$_SERVER
array. To see what you're sending to a web server, create a new PHP file and print out everything from the array.This will give you a nice representation of everything that's being sent to the server, from where you can extract the desired information, e.g.
$_SERVER['HTTP_USER_AGENT']
to get the operating system and browser.The code below could explain in its own right, how http://thismachine.info/ is able to show which operating system someone is using.
What it does is that, it sniffs your core operating system model, for example
windows nt 5.1
as my own.It then passes windows nt 5.1/i to Windows XP as the operating system.
Using:
'/windows nt 5.1/i' => 'Windows XP',
from an array.You could say guesswork, or an approximation yet nonetheless pretty much bang on.
Borrowed from an answer on SO https://stackoverflow.com/a/15497878/
Footnotes: (Jan. 19/14) There was a suggested edit on Jan. 18, 2014 to add
/msie|trident/i
by YJSoft a new member on SO.The comment read as:
Comment: because msie11's ua doesn't include msie (it includes trident instead)
I researched this for a bit, and found a few links explaining the Trident string.
Although the edit was rejected (not by myself, but by some of the other editors), it's worth reading up on the links above, and to use your proper judgement.
As per a question asked about detecting SUSE, have found this piece of code at the following URL:
Additional code:
Edit: April 12, 2015
I noticed a question yesterday that could be relevant to this Q&A and may be helpful for some. In regards to:
Mozilla/5.0 (Linux; Android 4.4.2; SAMSUNG-GT-I9505 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36
Another edit, and adding a reference link that was asked (and answered/accepted today, Nov. 4/16) which may be of use.
Consult the Q&A here on Stack: