如何在不编写脚本 (PHP) 或服务器配置 (Nginx) 的情况下检测移动设备(和/或移动 cookie)?
我们很快就会推出网站的移动版本。我们的完整网站和移动网站仅主题不同,即网址相同,唯一的区别在于前端。
当用户访问我们的网站时,我们需要能够执行以下操作:
1. 检查 cookie(移动 == true 或 false)以确定是否已定义完整与移动首选项(通过用户手动或通过我们端的检测)。
2. 如果未设置移动 Cookie,则在首页视图中检测用户的设备并将移动 Cookie 设置为 true 或 false。
3. 根据 #1 和/或 #2 的结果提供适当的体验(完整体验或移动体验)。
最初我使用 PHP 来检测工作正常的设备。然而,我们的网站在主页和其他一些页面上使用了极端完整的 HTML 缓存(.html 文件被写入我们的 Web 根目录中的文件夹中,如果 Nginx 找到它们,它们就会被提供服务,而不是通过 PHP 的请求 - 缓存每次都会被清除15 分钟),所以我不能依赖 PHP 从我们的主要用户入口点检测移动设备(据我目前所知......)。
由于无法依赖 PHP,所以我将移动 cookie 检查和设备检测放入 Nginx 配置文件中(开发时在本地使用 Apache,由我们的 Nginx 服务器人员翻译)。然而,我们的服务器管理人员回复我们说,新的 Nginx 配置文件对性能的影响将会很大(“性能影响”在我们的办公室是一个 4 个字母的词)。
基本上我被告知主页的完整 HTML 缓存必须保持不变,并且我根本无法更改 Nginx 配置文件。鉴于上述限制,我是否可以使用另一种 cookie/设备检测方法?
We are launching a mobile version of our website very soon. Our full website and mobile website are different only in theme, i.e. URLs are the same, only difference is on the front-end.
We need to be able to do the following when a user visits our site:
1. Check a cookie (mobile == true OR false) to determine if full vs. mobile preference has already been defined (by user manually or by detection on our end).
2. If no mobile cookie is set, detect user's device on first page view and set mobile cookie to true or false.
3. Serve the appropriate experience, full or mobile, based on results of #1 and/or #2.
Initially I was using PHP to detect devices which works fine. However, our site utilizes extreme full HTML caching on the home page and some other pages (.html files are written to a folder in our web root and if Nginx finds them they are served instead of the request going through PHP - cache is cleared every 15 minutes) so I cannot rely on PHP to detect a mobile device from our main point of user entry (as far as I know at this point...).
Not being able to rely on PHP, I then put the mobile cookie check and device detection into the Nginx configuration file (Apache locally for me while developing, translated by our server guy for Nginx). However, our server management folks got back to us saying the performance hit from the new Nginx configuration file would be large (and "performance hit" is a 4-letter word in our office).
Basically I'm being told full HTML caching of the home page has to stay in place and that I can't change the Nginx configuration file at all. Is there another method for cookie/device detection I could utilize given the restrictions in place from above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以使用 JavaScript。
试试这个:http://detectmobilebrowsers.com/
You could use javascript.
Try this: http://detectmobilebrowsers.com/
我过去曾在 CSS 媒体查询和 JavaScript 中使用过这个巧妙的技巧...
在您的网站关闭
标记之前:
在您的 CSS 文件中:
在您的 JavaScript 文件中(取决于 jQuery) ,但你可以修改它):
I've used this neat trick with CSS media queries and JavaScript in the past...
Right before your site's closing
<body>
tag:In your CSS file:
In your JavaScript file (depends on jQuery, but you could modify it):