Trying to detect a user’s browser with PHP only, is $_SERVER[‘HTTP_USER_AGENT’] a reliable way? Should I instead opt for the get_browser function? which one do you find brings more precise results?
If this method is pragmatic, is it ill advised to use it for outputting pertinent CSS links, for example:
if(stripos($_SERVER['HTTP_USER_AGENT'],"mozilla")!==false)
echo '<link type="text/css" href="mozilla.css" />';
I noticed this question, however I wanted to clarify whether this is good for CSS-oriented detection.
UPDATE:
something really suspicious: I tried echo $_SERVER['HTTP_USER_AGENT']; on IE 7 and this is what it output:
Mozilla/4.0 (compatible; MSIE 7.0;
Windows NT 6.0; SLCC1; .NET CLR
2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30618)
Safari gave something weird with “mozilla” in it too. What gives?
Using an existing method (ie
get_browser) is probably better than writing something yourself, since it has (better) support and will be updated with newer versions. There might be also usable libraries out there for getting the browser id in a reliable way.Decoding the
$_SERVER['HTTP_USER_AGENT']is difficult, since a lot of browsers have quite similar data and tend to (mis)use it for their own benefits. But if you really want to decode them, you could use the information on this page for all available agent ids.This page also shows that your example output indeed belongs to IE 7. More information about the fields in the agent id itself can be found on this page, but as I said already browsers tend to use it for their own benefits and it could be in a (slightly) other format.