手机浏览器检测? [英] Mobile browser detection?
问题描述
我们正在寻找一种确定用户是否正在使用移动浏览器的方法. 我们需要在PHP中通过解析用户代理字符串来做到这一点.我知道这种方法有很多警告,但是我们确实需要这样做.
We are looking for a way to determine if a user is using a mobile browser. We need to do that in PHP, by parsing the user agent string. I know this method has got many caveats, but we really need to do it that way.
您有什么建议吗?好的(即使不是完美的)更新代码?
Do you have any suggestion? A good (even if not perfect) updated code?
我了解WURFL,我相信它很棒,但是对于非开源项目,它不再是免费的. 通过仔细搜索,我还找到了以下代码: http://mobiforge.com /developing/story/lightweight-device-detection-php (以及一些变体),但我不确定.看起来好像写得很不好(例如,当他们使用$ mobile_browser ='0'并用引号引起来的整数时...).
I know about WURFL, and I believe it's great, but it's not free to use anymore for non open source projects. By googling a bit, I also found this code: http://mobiforge.com/developing/story/lightweight-device-detection-php (and some variations), but I'm not sure about it. Looks like it's written really bad (look, for example, when they use $mobile_browser = '0' with the quotes around an integer...).
你能推荐点什么吗?
谢谢
亚历山德罗
推荐答案
我正在使用这个:
$isMobile = (bool)preg_match('#\b(ip(hone|od)|android\b.+\bmobile|opera m(ob|in)i|windows (phone|ce)|blackberry'.
'|s(ymbian|eries60|amsung)|p(alm|rofile/midp|laystation portable)|nokia|fennec|htc[\-_]'.
'|up\.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\b#i', $_SERVER['HTTP_USER_AGENT'] );
它很短,可以检测大多数移动用户(或更确切地说是智能手机).由于iPad和Android平板电脑的屏幕尺寸较大,因此不会被归类为移动设备".
It's short and does detect most mobile users (or rather smartphones). iPad and Android-Tablets won't be classified as 'mobile' since they have bigger screen sizes.
如果您也想使用平板电脑,可以使用此功能:
If you want to catch Tablets as well, you can use this:
$isMobile = (bool)preg_match('#\b(ip(hone|od|ad)|android|opera m(ob|in)i|windows (phone|ce)|blackberry|tablet'.
'|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp|laystation portable)|nokia|fennec|htc[\-_]'.
'|mobile|up\.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\b#i', $_SERVER['HTTP_USER_AGENT'] );
这篇关于手机浏览器检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!