开发者

PHP mobile browser detection?

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.


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


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


(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


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.

  • http://en.wikipedia.org/wiki/List_of_user_agents_for_mobile_phones


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.


A quick Google search picks up these:

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

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


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.


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


Unfortunately WURFL is not free anymore for commercial projects.

But there is OpenDDR with its experimental PHP implementation.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜