php PHP手机浏览器检测?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3817155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP mobile browser detection?
提问by Trey Keown
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.
我需要一种检测移动浏览器服务器端的方法。我想要一种方法,它需要我很少进行设置和维护,但仍然为我提供(至少)Android、Mobile Safari 和 Blackberry 浏览器以及 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.
我希望至少覆盖大部分移动市场,如果不需要太多,我真的更喜欢几乎所有市场。
回答by kgiannakakis
回答by Ali Nouman
I found this one to be very easy to use php-mobile-detect
我发现这个非常容易使用php-mobile-detect
回答by VolkerK
(edit: for now the Browser Capability Project is closed, i.e. atm this answer is not an option)
(编辑:目前浏览器功能项目已关闭,即 atm 这个答案不是一个选项)
All you need is get_browser()and a recent browscap.ini that maps the user-agent string to a browser/version and its capabilities.
您所需要的只是get_browser()和最近的 browscap.ini,它将用户代理字符串映射到浏览器/版本及其功能。
You can get a usually very up-to-date browscap.ini version from http://browsers.garykeith.com/downloads.asp
您可以从http://browsers.garykeith.com/downloads.asp获得一个通常是最新的 browscap.ini 版本
回答by 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.
它只是读取标头(我如何读取 PHP 中的任何请求标头)并解析/解释它以读取“用户代理”的问题,您可能能够找到现有的 PHP 脚本,或者可能只是简单的正则表达式帮助确定哪些用户代理是移动的,哪些是普通的个人电脑/笔记本电脑。
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.
有很多不同的标头,因为它指示操作系统,所以有多少不同的移动操作系统就会有用户代理标头,所以脚本需要有一个所有有效标头的列表。
回答by Positronic Network
Found this library a little while back:
不久前发现了这个库:
http://code.google.com/p/mobileesp/
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.
有 PHP、Java、JavaScript 和 C# 版本。我认为它是一种“轻量级”智能手机/平板电脑检测工具,没有依赖性,并且会定期更新。尽管代码质量的某些方面有点不稳定,但我对此感到很复杂。
If you need something even lighter-weight, the WP Super Cache plugin for WordPress contains some long regex strings you could probably swipe.
如果您需要更轻量级的东西,WordPress 的 WP Super Cache 插件包含一些您可以轻扫的长正则表达式字符串。
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.
基于用户代理字符串的浏览器嗅探总是不稳定的。我查看了 WURFL,它压缩了几 MB。使用它是矫枉过正的。更好的方法是检测网站上使用的顶级设备,针对这些设备进行设计,然后收工。
回答by Gordon
Zend Framework introduced Zend_Http_UserAgentlately.
Zend Framework最近引入了Zend_Http_UserAgent。
It can determine the Mobile Device and it's capabilities by detecting the UserAgent through Wurfl, TeraWurfl and DeviceAtlas API.
它可以通过 Wurfl、TeraWurfl 和 DeviceAtlas API 检测 UserAgent 来确定移动设备及其功能。
回答by kurdtpage
回答by Adrian Ber
Unfortunately WURFL is not free anymore for commercial projects.
不幸的是,WURFL 对于商业项目不再免费。
But there is OpenDDRwith its experimental PHP implementation.
但是有OpenDDR及其实验性的 PHP 实现。
回答by Richard
How about http://code.google.com/p/hdapi/? Server side mobile detection in PHP.
http://code.google.com/p/hdapi/怎么样?PHP 中的服务器端移动检测。
回答by araldh
I once used to store temporarily visitors' ip-addresses and csrf-session values (meant to count current visitors).
我曾经用来临时存储访问者的 ip-addresses 和 csrf-session 值(旨在计算当前访问者)。
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.
而且我注意到我有一个额外的好处,即能够查看访问者使用的是移动设备还是 PC:移动设备的 csrf 会随着每个新页面的加载而变化,而 PC 不会;并且 ip 地址在这两种情况下都保留。
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.
当然,如果访问者点击两次,您只会检测到移动设备,我还没有检查它的可靠性(因为我并不真正需要它),但它只是 php/服务器端。