我正在尝试找到一个 php 函数,允许我查看用户正在使用的 Windows 版本,在你告诉我不是每个人都使用 Windows 之前,我正在使用这个函数来教育和为人们提供 Internet Explorer 的替代浏览器 - 仅限 Windows浏览器。
我希望能够检测到所有 9 个版本的 windows,这在 php 中可能吗?
我正在尝试找到一个 php 函数,允许我查看用户正在使用的 Windows 版本,在你告诉我不是每个人都使用 Windows 之前,我正在使用这个函数来教育和为人们提供 Internet Explorer 的替代浏览器 - 仅限 Windows浏览器。
我希望能够检测到所有 9 个版本的 windows,这在 php 中可能吗?
您可以尝试使用$_SERVER['HTTP_USER_AGENT']
.
在这里你可以找到一个很好的例子。
您可以使用
$_SERVER['HTTP_USER_AGENT'];
或者
$browser = get_browser(null, true);
print_r($browser);
检查get-browser on php.net
。
这是一个很好的代码来做你想做的事
<?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>";
}
?>