13

I'm in need of a way to detect mobile browsers server-side. I'd like a way that requires me to do little to set up and little to maintain, yet still provide me with accurate detection of (at the VERY least) Android, Mobile Safari and Blackberry browsers, along with alternatives like Opera.

I'd like to have at least the majority of the mobile market covered, and I'd really prefer virtually all of the market if it doesn't take much.

3
  • 2
    'little to maintain' is going to be difficult, as the only semi-reliable way we have are user-agent strings, which change all the time in new(er) versions. I do however wonder that 'collision' you anticipate? Commented Sep 28, 2010 at 21:24
  • I figured it would be. But oh, hah, that's the result of trying to post from an htc dream. Should just be "detection" Commented Sep 28, 2010 at 21:30
  • Very similar to stackoverflow.com/questions/6636306/mobile-browser-detection, Its accepted answer might just work for you, It's based on the suggessions by @Rick below, although it may require some maintenance Commented Feb 7, 2012 at 18:19

10 Answers 10

16

WURLF is the ultimate way for mobile browser detection and a PHP API is available.

Sign up to request clarification or add additional context in comments.

4 Comments

+1, let me just add that it gets updated frequently with new devices and device capabilities
I just wanted to note here (for any persons coming google after me) that the PEAR Log library is required for WURLF
Unfortunately, it appears that the PHP library is not updates as often as the Java one. I've just downloaded it, and it incorrectly recognize chrome on android as desktop. I'll look for a different lib
WURFL is no longer free for commercial use. They are charging an obscene amount per year to use their service
15

I found this one to be very easy to use php-mobile-detect

3 Comments

It's regularly updated, simple to use and sponsored by BrowserStack, too – looks like a decent library.
@tonyhb ... when you say it is regularly updated, I do not understand how it can be updated since I downloaded it (the Mobile_Detect.php file) to my computer. Do you mean that I would need to return to the download site routinely to check for newer versions?
Yeah, you'll need to re-download the file periodically to ensure you're up to date.
7

(edit: for now the Browser Capability Project is closed, i.e. atm this answer is not an option)

All you need is get_browser() and a recent browscap.ini that maps the user-agent string to a browser/version and its capabilities.

You can get a usually very up-to-date browscap.ini version from http://browsers.garykeith.com/downloads.asp

Comments

3

Its just a matter of reading the headers ( How do I read any request header in PHP ) and parsing / interpreting this to read the "user-agent", you may be able to find an existing PHP script or maybe just plain regex that will help in figuring out which user-agents are mobile and which are regular pc's / laptops.

There are a lot of different headers, as it indicates the operating system, so as many different mobile OS'es as there are there would be user-agent headers so the script needs to have a list of all valid ones.

2 Comments

Hey, thanks for the reply, but I was really more looking for a library to avoid this sort of tedious work...
You can just check the variable $_SERVER['HTTP_USER_AGENT']
3

Found this library a little while back:

http://code.google.com/p/mobileesp/

Has PHP, Java, JavaScript, and C# versions. I see it as a "lightweight" smartphone/tablet detection tool without dependencies and is regularly updated. I have mixed feelings about it though as some aspects of the code quality are a bit shaky.

If you need something even lighter-weight, the WP Super Cache plugin for WordPress contains some long regex strings you could probably swipe.

Browser sniffing based on user agent strings is always going to be flaky. I looked at WURFL and it is several MB compressed. Using that is overkill. A better approach is to detect the top devices in use on the website, design for those devices, and then call it a day.

Comments

2

A quick Google search picks up these:

http://mobiforge.com/developing/story/lightweight-device-detection-php

http://code.google.com/p/php-mobile-detect/

Comments

2

Zend Framework introduced Zend_Http_UserAgent lately.

It can determine the Mobile Device and it's capabilities by detecting the UserAgent through Wurfl, TeraWurfl and DeviceAtlas API.

Comments

1

How about http://code.google.com/p/hdapi/ ? Server side mobile detection in PHP.

Comments

1

Unfortunately WURFL is not free anymore for commercial projects.

But there is OpenDDR with its experimental PHP implementation.

Comments

0

I once used to store temporarily visitors' ip-addresses and csrf-session values (meant to count current visitors).

And I noticed that I had a side-gain of being able to see if the visitor used a mobile or a PC: The csrf of a mobile changes with every new page load, a PC doesn't; and the ip-addresses remain in both cases.

Sure, you only detect a mobile if a visitor clicks twice and I have not checked how reliable this is (as I do not really need it), but it is php/server-side only.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.