从浏览器检测确切的操作系统版本

IT技术 php javascript browser
2021-02-12 09:48:12

我想知道是否有一种方法可以使用 PHP/JS/ASP 从我的浏览器中检测到确切的操作系统版本?

我知道我可以检测操作系统的类型(Windows XP、Windows Vista、OS X 等),但我需要检测确切的版本:Vista Business、Vista Ultimate、Windows XP Home、Windows XP Pro 等...

6个回答

简短的回答:你不能。

长答案:

您所拥有的只是 HTTP User-Agent 标头中的信息,其中通常包含操作系统名称和版本。

通常,在 Mac OS 和 Linux 上运行的浏览器会发送足够的信息来识别确切的操作系统。例如,这是我的 User-Agent 标头:

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

您可以看到我正在运行 Ubuntu 8.10 Intrepid Ibex。

以下是 Firefox 和 Safari 4 Beta 在我的 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 浏览器通常只报告操作系统版本而不是特定的软件包(Pro、Business 等):

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

甚至还有非浏览器程序(ftp、下载管理器)允许用户设置他们将自己标识为的浏览器。
2021-03-14 09:48:12
也许它可能通过Flash?
2021-03-18 09:48:12
不需要万无一失的解决方案,但可以让我说 90% 的准确率。但事情似乎并非如此,这是不可行的。
2021-03-27 09:48:12
@Roy:也许吧,但我不会打赌。
2021-03-28 09:48:12
请注意,即使这也不是万无一失的。例如,更改用户代理字符串以报告其他内容在 Firefox(下载插件)中是微不足道的,而在 IE(注册表更改)中几乎微不足道。
2021-04-02 09:48:12

经过一番谷歌搜索后,我找到了这段代码,它似乎工作正常,(虽然没有检测到 Unix)

<?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;
?>
这不会检测到 Windows 的确切操作系统包(比如 xp home vista Ultimate)
2021-03-15 09:48:12
注意:eregi 已被弃用!
2021-03-19 09:48:12
@ReedRichards 你能在你找到这个的地方发帖吗?
2021-03-23 09:48:12
加上它不检测移动操作系统,这是现在非常需要的
2021-04-12 09:48:12
不是他真正想做的事吗?:)
2021-04-13 09:48:12

// 这会帮助你

$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);

在经典 ASP 和 ASP.NET 中使用

Request.ServerVariables("HTTP_USER_AGENT")

这效果最好,因为它不是解释代码,它在服务器上运行。

有工作代码或建议检查什么属性总是好的
2021-03-16 09:48:12

您真的应该尽量避免这样做,除非它对于 Web 应用程序的功能来说是绝对必要的。

意识到:

并非所有请求都可以来自在 Windows 上运行的客户端。

并非所有请求都可以来自支持 JavaScript 的客户端。

请求中可能不存在用户代理标头。

用户代理标头中的内容可能具有误导性。

设计良好的 Web 应用程序应提供完整的内容和功能,无论请求的用户代理标头中包含什么内容,或者客户端是否支持 javascript 或任何其他客户端扩展。

它是关于问题的有用信息,但不回答问题本身 (+0)。我同意你的看法,但由于 SO 有差异,有时需要检查它:例如:windows/linux 使用 Ctrl 进行多选,mac 使用 bowen-knot,如果你想向用户正确解释,你可能:1) 向双方解释所有内容,2) 测试操作系统。
2021-04-04 09:48:12
-1. 不回答 OP 问题。相反,给出一个不请自来的意见。
2021-04-06 09:48:12