Web Development » How to easily detect mobile devices

How to easily detect mobile devices

So this week I’ve been working on a pretty complex welcome video for a site that gets a decent amount of traffic (+100k visitors a month). We figured with all those visitors, there’s a good chance some of them are visiting on mobile devices. We checked the stats, and sure enough – we would need to provide content for them as well.

Since the iphone, ipod Touch and ipad, and possibly other mobile devices don’t allow flash, we decided to use Youtube for them… it’s non obtrusive and the videos will already be there as well… /shrug – why not? My favorite coding is done in php, so I was curious about the best way to detect a users browser server side, so we’re not sending extra code that may mess something up (things happen, ya know?) So here’s the approach I’ve adopted:

Click here to view the code on Snipplr.

A quick google search helped me find a full list of user agents on Wikipedia. So you might want to check that if you’re needing anything in addition to the 3 I’ve used above. Good luck, and happy coding!

Comments