PHP : 寻找 Chrome 和 Safari 浏览器 [英] Php : Finding Chrome and Safari Browsers
本文介绍了PHP : 寻找 Chrome 和 Safari 浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用下面的代码来查找用户代理,
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";
}
?>
但是我的 chrome 浏览器突然返回到 useragent 下面
But my chrome browser returning below useragent suddenly
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
它包含单词 safari 和 chrome.所以浏览器名称都被打印出来了.这是什么解决方案.谢谢.
It contains the word safari and chrome.so both the browser names are printed.what is the solution for this.thanks.
推荐答案
Chrome 的用户代理包含 Safari
但 Safari 的用户代理不包含 Chrome
所以使用 if ... elseif
:
Chrome's user agent contains Safari
but Safari's user agent doesn't contain Chrome
so use if ... elseif
:
if (stripos( $user_agent, 'Chrome') !== false)
{
echo "Google Chrome";
}
elseif (stripos( $user_agent, 'Safari') !== false)
{
echo "Safari";
}
注意:使用 stripos
而不是 strpos
来说明大小写变化.
Note: use stripos
instead of strpos
to account for case-variations.
这篇关于PHP : 寻找 Chrome 和 Safari 浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文