Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: get the browser name [closed]

Tags:

php

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.

like image 539
soupagain Avatar asked Feb 04 '10 13:02

soupagain


1 Answers

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?

like image 100
bobince Avatar answered Oct 27 '22 00:10

bobince