PHP mobile browser detection?

可紊 提交于 2019-11-27 04:16:49

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

Rick

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!