filmnut.org is a weblog about pop culture, technology and a dude named Jonathan. Welcome!

Wha' happened? An image is supposed to be right here.

You can also visit me on Twitter, Flickr, Last.FM, Vimeo, Delicious, Wishlistr, Lala, Google Reader, and Friendfeed.

Twitter Status:

Last Three Movies I've Seen: Alice in Wonderland, A Single Man, and It's Complicated

Book I'm Currently Reading: A Separate Peace by John Knowles

My Eight Most Recent Flickr Pics:

Lala Playlist I've Been Enjoying:

Last 10 Songs I've Listened To:

Links I've Recently Enjoyed:

Using PHP to Detect a User's Browser

I needed a quick way to detect what browser any given user visiting my site was using. I used this information to serve them a custom CSS stylesheet. And sure, PHP claims to have a built-in method to achieve this functionality: get_browser. But I ran into problems with that dude: it's slow, it returns way more info than I need (an array of data the tells me everything the browser is capable of), and, the nail in the coffin, it didn't work on my web server (I get a "browscap ini directive not set" error).

Instead of dealing with these issues (which, I believe, would mean calling GoDaddy, who runs my server), I built my own little custom method. I'll show it off after the break. Here's the script.



Download the .php file here.

I'm simply using PHP's Switch feature, which seems to work pretty well. Feel free to borrow and improve it.

Disclaimer: I'm not a PHP expert. I'm sure someone (everyone?) will come along and say this is wildly incorrect, but, hey, it works for me and it is reasonably efficient (thanks to the breaks). In other words, I'm not claiming it's perfect, I'm saying it works for me and I thought I'd share.
3/26/2009
You are reading the Filmnut Blog edited by Jonathan. Want an RSS feed? Some rights reserved.