2007-12-09

Mobile Browser Detection in PHP

From Russell Beattie:

"I use the fastest method of matching strings in PHP, which is strpos() - it returns False if it doesn't find a match, or the number of the first character in that match. It's fast, and you'll see in the code below, chainable using the || (or) comparison operator. As soon as a match comes up, it'll short circuit the rest of the code, making common user-agents faster to detect with a simple re-ordering. Also, you can mix and match headers to look at including remote address, etc. For Mowser, I also like to make sure that bots always see the mobile version as well, so I've included detection code for those user-agents (and one common IP) at the bottom."