PHP:获取浏览器名称 [英] PHP: get the browser name

查看:215
本文介绍了PHP:获取浏览器名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用PHP获取浏览器名称?我以为这会很简单吗?我需要做的就是区分IE和Firefox.

How do I get the browser name using PHP? I thought this would be straightforward? All I need to do is differentiate between IE and Firefox.

推荐答案

if (strpos($_SERVER['HTTP_USER_AGENT'], '(compatible; MSIE ')!==FALSE) {
    ...ie specific...
}

但是! 不要!

很少有充分的理由在服务器端嗅探用户代理.它带来了很多问题,包括:

There is rarely a good reason to be sniffing user-agent at the server side. It brings a bunch of problems, including:

  • 浏览器和其他有关用户身份的用户代理,或者完全剥离用户代理标头,或者通常很难从标头文本中区分出真正的浏览器是什么.例如,当欺骗IE时,上面的规则还将检测Opera,以及IEMobile(Windows Mobile),您可能想要也可能不想要,因为它与台式机IE完全不同.

  • browsers and other user-agents that lie about who they are, or strip the user-agent header completely, or generally make it hard to distinguish what the real browser is from the header text. For example the above rule will also detect Opera when it's spoofing IE, and IEMobile (Windows Mobile), which you may or may not want as it is a very different browser to desktop IE.

如果在服务器端区分用户代理,则必须在响应中返回Vary: User-Agent标头,否则代理可能会缓存该页面的版本并将其返回给不支持该功能的其他浏览器匹配.但是,包含此标头具有使IE中的缓存混乱的副作用.

if you discriminate on the user-agent at the server-side, you must return a Vary: User-Agent header in the response, otherwise proxies may cache a version of the page and return it to other browsers that don't match. However, including this header has the side-effect of messing up caching in IE.

根据您要实现的目标,几乎总是有一种更好的方法,可以使用CSS hacks,JScript或

Depending on what it is you are trying to achieve, there is almost always a much better way of handling the differences between IE and other browsers at the client side, using CSS hacks, JScript or conditional comments. What is the real purpose for trying to detect IE in your case?

这篇关于PHP:获取浏览器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆