自动检测移动浏览器(通过用户代理?) [英] Auto detect mobile browser (via user-agent?)

查看:93
本文介绍了自动检测移动浏览器(通过用户代理?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测如果用户是从移动网络浏览器浏览我的网站,这样我就可以自动检测并显示我的网站的相应版本?

How can I detect if a user is viewing my web site from a mobile web browser so that I can then auto detect and display the appropriate version of my web site?

推荐答案

是的,看完User-Agent头就可以了。

Yes, reading the User-Agent header will do the trick.

有一些名单中的有已知的移动用户代理的,所以你不必从头开始。我所做的,当我不得不是因为它们修订检测​​,然后手工计算出它们是什么打造知名用户代理和专卖店未知的数据库。这最后一件事可能是在某些情况下矫枉过正。

There are some lists out there of known mobile user agents so you don't need to start from scratch. What I did when I had to is to build a database of known user agents and store unknowns as they are detected for revision and then manually figure out what they are. This last thing might be overkill in some cases.

如果你想在Apache的水平,这样做,你可以创建定期生成一组重写规则检查用户代理(或只有一次的脚本,而忘记了新的用户代理,或每月一次,任何适合你的情况),像

If you want to do it at Apache level, you can create a script which periodically generates a set of rewrite rules checking the user agent (or just once and forget about new user agents, or once a month, whatever suits your case), like

RewriteEngine On

RewriteCond %{HTTP_USER_AGENT} (OneMobileUserAgent|AnotherMobileUserAgent|...)
RewriteRule (.*) mobile/$1

这将移动,例如,要求到 HTTP://domain/index.html 来的http://domain/mobile/index.html

如果你不喜欢有一个脚本定期重新建立一个htaccess文件的方法,你可以写一个检查用户代理模块(我没有找到一个已经作出,但发现这种特别适合的example ),并从一些网站的用户代理更新它们。然后,你可以,只要你想尽可能多的方法复杂,但我认为你的情况previous方法就可以了。

If you don't like the approach of having a script recreate a htaccess file periodically, you can write a module which checks the User Agent (I didn't find one already made, but found this particularly appropriate example) and get the user agents from some sites to update them. Then you can complicate the approach as much as you want, but I think in your case the previous approach would be fine.

这篇关于自动检测移动浏览器(通过用户代理?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆