本文介绍了如何在没有脚本(PHP)或服务器配置(Nginx)的情况下检测移动设备(和/或移动cookie)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们即将推出我们网站的移动版本.我们的完整网站和移动网站仅主题不同,即URL相同,只是前端不同.

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..检查Cookie(移动设备== true或false),以确定是否已经定义了完全还是移动设备偏好设置(通过用户手动还是通过我们的检测).

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..如果未设置移动Cookie,请在首页浏览中检测用户的设备,并将移动Cookie设置为true或false.

2. If no mobile cookie is set, detect user's device on first page view and set mobile cookie to true or false.

3..根据#1和/或#2的结果,提供适当的完整或移动体验.

3. Serve the appropriate experience, full or mobile, based on results of #1 and/or #2.

最初,我使用PHP来检测运行良好的设备.但是,我们的网站在主页和其他一些页面上使用了极其完整的HTML缓存(.html文件被写入到我们的Web根目录中的文件夹中,如果Nginx找到了它们,则将它们提供给服务,而不是通过PHP进行请求-缓存会在每次清除时清除15分钟),所以我不能依靠PHP从用户输入的主要角度来检测移动设备(据我所知……).

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...).

由于无法依赖PHP,因此我将移动cookie检查和设备检测放入Nginx配置文件(开发时在我本地使用Apache,由Nginx的服务器人员翻译).但是,我们的服务器管理人员再次与我们联系,说新的Nginx配置文件对性能的影响会很大(在我们的办公室中,性能影响"是一个4个字母的单词).

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).

基本上,我被告知必须对主页进行完整的HTML缓存,而我根本无法更改Nginx配置文件.鉴于上述限制,我还可以使用另一种方法来检测Cookie/设备吗?

推荐答案

您可以使用javascript.

You could use javascript.

尝试以下操作: http://detectmobilebrowsers.com/

这篇关于如何在没有脚本(PHP)或服务器配置(Nginx)的情况下检测移动设备(和/或移动cookie)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-24 15:23