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.

Charles
  • 50,943
  • 13
  • 104
  • 142
Trey Keown
  • 1,345
  • 3
  • 16
  • 23
  • 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? – Wrikken Sep 28 '10 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" – Trey Keown Sep 28 '10 at 21:30
  • Very similar to http://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 – Sydwell Feb 07 '12 at 18:19

10 Answers10

16

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

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • +1, let me just add that it gets updated frequently with new devices and device capabilities – Fanis Hatzidakis Sep 28 '10 at 21:45
  • 1
    I just wanted to note here (for any persons coming google after me) that the PEAR Log library is required for WURLF – electblake Jan 27 '11 at 15:55
  • 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 – Moshe Shaham Aug 06 '12 at 06:42
  • 1
    WURFL is no longer free for commercial use. They are charging an obscene amount per year to use their service – Ryan May 09 '13 at 07:07
15

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

Ali Nouman
  • 3,304
  • 9
  • 32
  • 53
  • It's regularly updated, simple to use and sponsored by BrowserStack, too – looks like a decent library. – tonyhb Sep 10 '12 at 11:38
  • @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? – dsdsdsdsd Jan 18 '13 at 14:29
  • Yeah, you'll need to re-download the file periodically to ensure you're up to date. – tonyhb Jan 28 '13 at 15:31
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

VolkerK
  • 95,432
  • 20
  • 163
  • 226
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.

Community
  • 1
  • 1
Rick
  • 16,612
  • 34
  • 110
  • 163
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.

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/

andrewsi
  • 10,807
  • 132
  • 35
  • 51
kurdtpage
  • 3,142
  • 1
  • 24
  • 24
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.

Gordon
  • 312,688
  • 75
  • 539
  • 559
1

Unfortunately WURFL is not free anymore for commercial projects.

But there is OpenDDR with its experimental PHP implementation.

Adrian Ber
  • 20,474
  • 12
  • 67
  • 117
1

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

Richard
  • 279
  • 3
  • 4
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.

araldh
  • 51
  • 6