Detecting browser in php for css
In designing websites, I've discovered that the old problems with Internet Explorer are still around, mainly that you can code a beautiful website only to discover that Internet Explorer doesn't support the coolest things you've done.
The reasons behind this are multiple, but the short version is that Microsoft wanted to own the Internet, so it refused to participate in Web standards by having their browser support them, forcing webpage designers to make concessions for Internet Explorer when they designed. The hope was that they'd just throw up their hands, use Internet Explorer's proprietary coding and eventually everyone would have to use Internet Explorer (and Windows) if they wanted to surf the Web.
But that backfired, and Internet Explorer is losing Web presence daily. That said, the newest Web standards are unevenly implemented across browsers, with Safari being the only browser I've found so far that actually implements CSS3 properly. The problem is, JavaScripts designed to detect browsers are woefully out of date and oftentimes provide wrong information, meaning you have to put in a boatload of effort to code a page to detect which browser you're using, and then it's tough to implement whatever changes you make throughout the page.
This stuck out like a sore thumb on one of the latest sites I was coding, Square Deal Music, for which I had coded all sorts of cool stuff, including gradient overlays in the div containers, which were not supported on Firefox, I discovered to my chagrin. That means Firefox users would see my dark text against a dark background, making it quite difficult to read - and ugly to boot.
So Friday I came up with a solution: I would code one CSS stylesheet for Safari (which adheres completely to Web standards) and another for every other browser. Then, in php, I would detect the browser and print the link for the correct stylesheet based on which browser was being used:
if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') ){
$Safari = TRUE;
echo "<link rel=\"stylesheet\" href=\"Safari.css\">";
} else {
$Safari = FALSE;
echo "<link rel=\"stylesheet\" href=\"BadBrowser.css\">";
}
strpos returns the position of the quoted string in the passed string. If the string does not exist inside the larger string, it returns FALSE, which is why I use it here as kind of a poor man's grep or regex. I'm just testing to see if the string "Safari" exists in the user-agent field the HTTP server passes to php.
A bonus of this approach is the $Safari variable is then available through the rest of the page so I can implement all sorts of things, like popping up a div to alert the viewer that they can download a free browser to see the site as it was intended. Like this:
if ($Safari == FALSE) {
echo "<div class=\"badbrowser\"><h2>BAD BROWSER! You are viewing a crippled version of this site because your browser is not standards-compliant. Download <a href=http://apple.com/safari>Safari</a> for free and you can see this site as it was intended.</h2></div>";
}
Fun, eh?