Browser Detection

Hi guys,

Whats the best method of browser detection in PHP?
Are there any good open source scripts or tutorials I could take a look at?


The problem is, most browsers either allow you to edit the user-agent value or they fake a UA string to prevent those “upgrade your browser” scripts (mobile devices).

Accuracy on those redirection tools is just plain poor, you would have more success using CSS3 media queries to restyle the layout based on the available viewport than to use those things. Not only are you increasing the overhead (processing the hundreds of thousands of UA strings), but you’re assuming that your visitors want the other site (they might need something only available on the full design - or may only want to use that version). It’s blind decision making for devices you don’t know exist… absolutely crazy. Even if the script you have is up-to-date now, new devices are released weekly (around the world) that’ll need detection! :slight_smile:

PS: elricho, you’re entirely correct on the tablet issue… ironically people seem to think that the experience on an iPhone will be equal to an iPad (oh well).

why would i want to pay monthly for mobile detection ? i wrote a script that does the same thing in 10 minutes.

Check out - Its a PHP APi Kit that interfaces with Handset Detection for mobile browser detection (disclaimer : I work there).


Script work very effectively than paying for such site.
I suggest don’t pay. You can use scripts over there and make that as per your choice.
you can do, what you wanna.

You could take a look at

Or browscap.ini/ get_browser:

The second method is the best/ fastest, but is not supported on all servers.
The first one is a little slower, but it supported on all servers.

Both require maintenance to keep the browser lists up-to-date.

If you go the static script route its a good idea to incorporate a system of feedback so you can catch any regexp errors or new handsets. Some process that logs the useragent and if it redirected or not (then sort & uniq that - linux command line). Sure you can grep through webserver logs, but thats no fun and what you really want to see is the outcome (redirection or not).


Good question. Well firstly up to 10000 detections is free.

Secondly its a trade off between accuracy, time and cost right. There’s 56000+ mobile user agents from over 280 vendors, growing at 80+ mobile user agents per day.

Any quick regexp script might get 80%, after a day or two of work it might get 90%. Perhaps thats fine … but what happens next month ? When a new phone comes out, or Android tablets come out (Samsung Galaxy next month) ? You dont want to redirect tablets, but you want to redirect mobiles and most android user-agents only vary a few characters … Then lets say you’re a web designer with 10 sites that have mobile redirection - do you want to be updating that script every other week or other month on all sites ?

I’ve been there and a quick regexp sounds easy but the only rock solid way is a full user agent match against a known user agent, and the only way to future proof that is to run it against a service being updated all the time. For some people that trade off is well worth the cost (and they mark that up & pass on to their clients anyhow).