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...
}
But! Don't!
There is rarely a good reason to be sniffing user-agent at the server side. It brings a bunch of problems, including:
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.
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With