20

I was wondering if there is a way I can detect the exact Operating System version from my browser using PHP/JS/ASP?

I know I can detect the type of OS (Windows XP,Windows Vista,OS X,etc) but I need to detect the exact version: Vista Business, Vista Ultimate, Windows XP Home, Windows XP Pro, etc...

asked Mar 15, 2009 at 15:19
0

12 Answers 12

26

Short answer: You can't.

Long answer:

All you have is the information in the HTTP User-Agent header, which usually contains the OS name and version.

Usually, browsers running on Mac OS and Linux send enough information to identify the exact OS. For example, here's my User-Agent header:

Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.7) Gecko/2009030423 Ubuntu/8.10 (intrepid) Firefox/3.0.7

You can see that I'm running Ubuntu 8.10 Intrepid Ibex.

And here's what Firefox and Safari 4 Beta report on my MacBook Pro:

Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.7) Gecko/2009021906 Firefox/3.0.7

Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/528.16 (KHTML, like Gecko) Version/4.0 Safari/528.16

Windows browsers, on the other hand, usually only report the OS version and not the specific package (Pro, Business, etc.):

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:x.x.x) Gecko/20041107 Firefox/x.x

answered Mar 15, 2009 at 15:24
Sign up to request clarification or add additional context in comments.

6 Comments

Maybe its possible via Flash?
@Roy: maybe, but I wouldn't bet on it.
And note that even this isn't foolproof. For example, changing your user-agent string to report something else is trivial in Firefox (download plugin) and almost trivial in IE (registry change).
Don't need a foolproof solution, but something which will give me say 90% accuracy. But as things seems not, its not feasible.
There are even non-browser programs (ftp, download managers) that allow the user to set the browser they will identify themselves as.
|
17

After some googling I found this code and it seems to be working ok,(doesn't detect Unix though)

<?php 
$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using ...
echo "You are using ".$CurrOS;
?>
shybovycha
12.4k6 gold badges54 silver badges86 bronze badges
answered Apr 26, 2009 at 20:03

6 Comments

Not really what he wanted to do though is it? :)
This will not detect the exact OS package(say xp home vista ultimate ) of windows
plus it doesn't detect mobile OS, which are quite required nowadays
CAUTION: eregi is deprecated!
@ReedRichards are you able to post where you found this?
|
14

// this will help you

$uagent = $_SERVER['HTTP_USER_AGENT'] . "<br/>";
function os_info($uagent)
{
 // the order of this array is important
 global $uagent;
 $oses = array(
 'Win311' => 'Win16',
 'Win95' => '(Windows 95)|(Win95)|(Windows_95)',
 'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)',
 'Win98' => '(Windows 98)|(Win98)',
 'Win2000' => '(Windows NT 5.0)|(Windows 2000)',
 'WinXP' => '(Windows NT 5.1)|(Windows XP)',
 'WinServer2003' => '(Windows NT 5.2)',
 'WinVista' => '(Windows NT 6.0)',
 'Windows 7' => '(Windows NT 6.1)',
 'Windows 8' => '(Windows NT 6.2)',
 'Windows 8.1' => '(Windows NT 6.3)',
 'Windows 10' => '(Windows NT 10.0)',
 'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
 'OpenBSD' => 'OpenBSD',
 'SunOS' => 'SunOS',
 'Ubuntu' => 'Ubuntu',
 'Android' => 'Android',
 'Linux' => '(Linux)|(X11)',
 'iPhone' => 'iPhone',
 'iPad' => 'iPad',
 'MacOS' => '(Mac_PowerPC)|(Macintosh)',
 'QNX' => 'QNX',
 'BeOS' => 'BeOS',
 'OS2' => 'OS/2',
 'SearchBot' => '(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
 );
 $uagent = strtolower($uagent ? $uagent : $_SERVER['HTTP_USER_AGENT']);
 foreach ($oses as $os => $pattern)
 if (preg_match('/' . $pattern . '/i', $uagent))
 return $os;
 return 'Unknown';
}
echo os_info($uagent);
answered Jul 1, 2012 at 3:52

Comments

2

In Classic ASP and ASP.NET use

Request.ServerVariables("HTTP_USER_AGENT")

This works best since it's not interpreted code, this is running on the server.

answered Mar 15, 2009 at 16:27

1 Comment

always good to have working code or suggestion for what property to examine
1

You should really try to avoid doing something like that unless it's absolutely necessary for the functionality of the web application.

Be aware that:

Not all requests can come from a client running on windows.

Not all requests can come from a client that supports JavaScript.

The user-agent header may not be present in the request.

What is in the user-agent header may be missleading.

A well designed web application should provide complete content and functionality regardless of what's in the user-agent header of the request or if the client supports javascript or any other clientside extension.

answered Mar 15, 2009 at 15:54

2 Comments

-1. Doesn't answer the OPs question. Gives an unsolicited opinion instead.
It is useful information about the question, but does not answer the question itself (+0). I agree with you, but as S.O. has differences, some times it is necesary to check it: e.g: windows/linux use Ctrl for multi-select, mac use bowen-knot, if you want to explain that correctly to the user, you may: 1) explain all to both, 2) test the OS.
1

I don't think you'll be able to differentiate different versions of Vista, but you should be able to get the OS from the navigator object's platform property. You'll probably have to parse it, though, or differentiate based on it's contents.

<script type="text/javascript">
 alert( navigator.platform );
</script>

See www.w3schools.com tutorials for an example showing how to get all the navigator's properties.

EDIT:

To get the exact version, you may be able to develop an ActiveX control (Windows only) or Java Applet and use the java.lang.System object to view the current system properties. You may be able to get enough information from the browser for non-Windows systems and use the control only for Windows systems. I haven't tried this, so you'd need to experiment to see if it would work. I have to believe that Microsoft's ActiveX control for Microsoft Update is able to detect the exact system version and installed software for it to work.

answered Mar 15, 2009 at 15:28

1 Comment

As you said, this won't help to differentiate the sub versions, but thanks for the snippet :-)
1

As others have already said, no, not reliably.

That is the reason that for example jquery has switched to a browser-capabilities system (for lack of a better word) instead of a browser-sniffing system for it ́s functionalities.

answered Mar 15, 2009 at 16:06

1 Comment

Browser cababilities or browser-sniffing as you say are BROWSER related, not S.O related. As an example, Firefox 24 as same capabilities on Linux, Mac or Windows. This question is about S.O., not browsers.
1

I've created a jquery function which does this. This way we can track all the OS using navigator.

Hope someone will get help from this:

function find_os_version() {
 var ua = navigator.userAgent.toLowerCase();
 if (ua.indexOf("windows nt 5.0") != -1) {
 return 'win2k';
 }
 if (ua.indexOf("windows nt 5.1") != -1) {
 return 'winXP';
 }
 if (ua.indexOf("windows nt 6.0") != -1) {
 return 'winVista';
 }
 if (ua.indexOf("windows nt 6.1") != -1) {
 return 'win7';
 }
 if (ua.indexOf("windows nt 6.2") != -1) {
 return 'win8';
 }
}
answered Mar 13, 2013 at 7:35

Comments

1

This might be the easiest way:

Download library from http://mobiledetect.net

Put Mobile_Detect.php in to 'libraries' if using CI or just include it.

Use this code in PHP

$detect = new Mobile_Detect();
// Get the version() of components.
$detect->version('iPad'); // 4.3 (float)
$detect->version('iPhone') // 3.1 (float)
$detect->version('Android'); // 2.1 (float)
$detect->version('Opera Mini'); // 5.0 (float)

Find documentation on http://dwij.co.in/mobile-os-detection-in-php-codeigniter

answered Dec 11, 2013 at 12:56

Comments

0

in CodeIgniter you can find library called "user_agent". It will give you all you need really. If you're not using CI, you can still "extract" the code for your framework. Hope that helps. http://codeigniter.com/user_guide/libraries/user_agent.html

answered Jan 25, 2012 at 17:27

Comments

0

UPDATE 2025.

I tested all of the solutions above and they don't work anymore. eregi is indeed deprecated.

I modified the above first answer to be functional again (I only tested with windows 10 so you can add newer versions of apple, Iphone, Linux, etc if required):

$OSList = array(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows 7' => '(Windows NT 7.0)',
'Windows 10' => '(Windows NT 10.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
$data = $_SERVER['HTTP_USER_AGENT'];
$keys = array_keys($OSList);
$i=0;
foreach($OSList as $CurrOS){
 echo "<br/>testing: ".$CurrOS;
 if (preg_match(("/".$CurrOS."/i"), $data)){
 echo "<br/>We found: ".$keys[$i];
 // break
 break;
 }
 $i+=1;
}

Otherwise there is a very good approach below:

Something that still works pretty fine is this github browser class repo from cb schuld (Credits to his amazing work - Something like 2000 lines of code). It cannot say the exact OS version but still give the general OS which is pretty cool

It can find, browser type, if it's a tablet it it's a mobile, and also what platform is actually used. I tested it and it works perfectly . Download the browser.php class and do the following

include('browser.php');
$browser = new Browser();
//To get the platform (Linux, Windows, Apple, IPhone, Android)
$browser->getPlatform();
//Not in the question but actually really cool to know
//To get the browser
$browser->getBrowser();
//To get the browser version
$browser->getVersion();
//To know if it's a mobile device
$isMobile = $browser->isMobile();
//To know if it's a tablet
$isTablet = $browser->isTablet();
//To know if it's a robot
$isRobot = $browser->isRobot();

It returns boolean values for the isRobot(), isMobile(), isTablet() isFacebook() (Facebook Browser) functions just so you know

answered May 16, 2025 at 17:37

Comments

-2

Although the question is very old but i am just answering it:

just try:

$this->agent->platform()

this will give you output as: Linux, Windows, OS X, etc.

for more info visit:

http://ellislab.com/codeigniter/user-guide/libraries/user_agent.html

answered Jan 22, 2014 at 22:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.