PHP : 寻找 Chrome 和 Safari 浏览器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15272084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Php : Finding Chrome and Safari Browsers
提问by Vishnu Vishwa
I use below code to find user agent,
我使用下面的代码来查找用户代理,
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match('/MSIE/i', $user_agent)) {
echo "Internet Explorer";
}
if (preg_match('/Firefox/i', $user_agent)) {
echo "FireFox";
}
if (strpos( $user_agent, 'Chrome') !== false)
{
echo "Google Chrome";
}
if (strpos( $user_agent, 'Safari') !== false)
{
echo "Safari";
}
if (preg_match('/Opera/i', $user_agent)) {
echo "Opera";
}
?>
But my chrome browser returning below useragent suddenly
但是我的 chrome 浏览器突然返回到 useragent 下面
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.155 Safari/537.22
It contains the word safari and chrome.so both the browser names are printed.what is the solution for this.thanks.
它包含单词 safari 和 chrome。所以两个浏览器名称都被打印出来。这是什么解决方案。谢谢。
回答by MichaelRushton
Chrome's user agent contains Safaribut Safari's user agent doesn't contain Chromeso use if ... elseif:
Chrome 的用户代理包含Safari但 Safari 的用户代理不包含Chrome所以使用if ... elseif:
if (stripos( $user_agent, 'Chrome') !== false)
{
echo "Google Chrome";
}
elseif (stripos( $user_agent, 'Safari') !== false)
{
echo "Safari";
}
Note: use striposinstead of strposto account for case-variations.
注意:使用stripos代替strpos来说明案例变化。
回答by Prasanth Bendra
Try this :
尝试这个 :
$browser = get_browser(null, true);
print_r($browser);
From doc : Attempts to determine the capabilities of the user's browser, by looking up the browser's information in the browscap.ini file.
来自 doc :尝试通过在 browscap.ini 文件中查找浏览器信息来确定用户浏览器的功能。

