0

I am trying to find a php function allowing me to see what version of windows a user is using, and before you tell me not everyone uses windows, I am using this function to educate and give people alternative browsers to Internet explorer - A windows only browser.

I would like to be able to detect all 9 versions of windows, is this possible in php?

user2751288
  • 188
  • 1
  • 17

3 Answers3

3

You can try with $_SERVER['HTTP_USER_AGENT']. Here you can find a good example.

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
guillefd
  • 1,903
  • 1
  • 16
  • 26
2

You can use

$_SERVER['HTTP_USER_AGENT'];

Or

$browser = get_browser(null, true);
print_r($browser);

Check get-browser on php.net.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • This does not deal with the OS detection as well nor does it deal with the complications of verifing the agent string. – Ronni Skansing Oct 05 '13 at 12:01
  • @QuackQuacker, These does but at some extent, you can't rely on, [check this answer](http://stackoverflow.com/questions/2670023/is-it-possible-to-detect-what-operating-system-a-user-is-coming-from-using-php) and read all notes about `get-browser` on manual. – The Alpha Oct 05 '13 at 15:35
  • That answer does also not handle OS versions, if you look at the method you can see it clearly just return Windows if it windows and not Windows 8 if it is windows 8. Take a look at the User agents internet explore uses. http://www.useragentstring.com/pages/Internet%20Explorer/ – Ronni Skansing Oct 05 '13 at 16:04
  • 1
    @QuackQuacker, Check [get_browser](http://us3.php.net/get_browser), `$array['platform']` and read the note about `browscap`. – The Alpha Oct 05 '13 at 16:09
  • 1
    You are correct, missed that part. – Ronni Skansing Oct 05 '13 at 16:18
1

Here is a nice bit of code to do what you want

<?php
$OSList = array
(
        // Match user agent string with operating systems
        '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 6.1)',
        'Windows 8' => '(Windows NT 6.2)',
        'Windows 8.1' => '(Windows NT 6.3)',
        'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
        'Windows ME' => 'Windows ME'
);

// 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;
        }
}
echo "We detect you are using ".$CurrOS."<br style='clear:both'>";
if ($CurrOS == "Windows XP")
{
echo "The alternative browsers you can download are:<br style='clear:both'><a target='_blank' href='http://google.com/chrome' style='color:white'>Google Chrome</a><br style='clear:both'><a target='_blank' href='http://mozilla.org/firefox' style='color:white'>Mozilla Firefox</a><br style='clear:both'><a target='_blank' href='http://opera.com' style='color:white'>Opera<br>"; 
}
elseif ($CurrOS == "Windows Vista")
{
echo "The alternative browsers you can download are:<br style='clear:both'><a target='_blank' href='http://google.com/chrome' style='color:white'>Google Chrome</a><br style='clear:both'><a target='_blank' href='http://mozilla.org/firefox' style='color:white'>Mozilla Firefox</a><br style='clear:both'><a target='_blank' href='http://opera.com' style='color:white'>Opera<br>"; 
}
elseif ($CurrOS == "Windows 7")
{
echo "The alternative browsers you can download are:<br style='clear:both'><a target='_blank' href='http://google.com/chrome' style='color:white'>Google Chrome</a><br style='clear:both'><a target='_blank' href='http://mozilla.org/firefox' style='color:white'>Mozilla Firefox</a><br style='clear:both'><a target='_blank' href='http://windows.microsoft.com/en-us/internet-explorer/ie-10-worldwide-languages' style='color:white'>Internet Explorer 10</a><br style='clear:both'><a target='_blank' href='http://windows.microsoft.com/en-us/internet-explorer/ie-11-worldwide-languages' style='color:white'>Internet Explorer 11</a><br style='clear:both'><a target='_blank' href='http://opera.com' style='color:white'>Opera<br>"; 
}
elseif ($CurrOS == "Windows 8")
{
echo "The alternative browsers you can download are:<br style='clear:both'><a target='_blank' href='http://google.com/chrome' style='color:white'>Google Chrome</a><br style='clear:both'><a target='_blank' href='http://mozilla.org/firefox' style='color:white'>Mozilla Firefox</a><br style='clear:both'><a target='_blank' href='http://windows.microsoft.com/en-us/internet-explorer/ie-10-worldwide-languages' style='color:white'>Internet Explorer 10</a><br style='clear:both'><a target='_blank' href='http://opera.com' style='color:white'>Opera<br>"; 
}
elseif ($CurrOS == "Windows 8.1")
{
echo "The alternative browsers you can download are:<br style='clear:both'><a target='_blank' href='http://google.com/chrome' style='color:white'>Google Chrome</a><br style='clear:both'><a target='_blank' href='http://mozilla.org/firefox' style='color:white'>Mozilla Firefox</a><br style='clear:both'><a target='_blank' href='http://windows.microsoft.com/en-us/internet-explorer/ie-11-worldwide-languages' style='color:white'>Internet Explorer 11</a><br style='clear:both'><a target='_blank' href='http://opera.com' style='color:white'>Opera<br>"; 
}
elseif ($CurrOs == "Windows ME" || $CurrOs == "Windows 98" || $CurrOs == "Windows 2000")
{
echo "The alternative browsers you can download are:<br style='clear:both'><a target='_blank' href='http://opera.com' style='color:white'>Opera<br>";   
}
else
{
    echo "<br>The version of windows you are currently using is not supported by any browsers better than Internet Explorer. We recommend you upgrade to a Windows XP, 7 or 8 machine to enjoy the best of the web<br>";
}

?>

user2751288
  • 188
  • 1
  • 17
  • This is the code from the link i supplied. Added with a too big if else. http://www.geekpedia.com/code47_Detect-operating-system-from-user-agent-string.html – Ronni Skansing Oct 05 '13 at 12:13