Ensuring that websites can adapt to multiple devices – particularly mobile devices – has become both increasingly important and complex. The variety of ways in which people browse the web having mushroomed. Much of this has been driven by the rise in the use of mobile devices to access the web, although it also applies to devices such as tablets, Internet TV, and so on.
确保网站可以适应多种设备(尤其是移动设备)已经变得越来越重要和复杂。 人们浏览网络的各种方式如雨后春笋般冒出。 尽管移动设备也适用于平板电脑,互联网电视等设备,但其中很大一部分是由使用移动设备访问网络的兴起推动的。
In recent years, Responsive Web Design has become all the rage as a mechanism for adapting websites and applications to cater to multiple devices by using information client-side to adapt layouts accordingly. In a sense, this is a “one-size-fits-all” approach. However, there’s often a case for providing different versions of the same site or using a slightly different approach.
近年来,作为一种通过使用信息客户端相应地调整布局来适应网站和应用程序以适应多种设备的机制,响应式Web设计已成为一种流行。 从某种意义上说,这是“一刀切”的方法。 但是,通常存在提供同一网站的不同版本或使用略有不同的方法的情况。
An alternative solution to the problem is to use server-side device detection and then take certain actions based on that information. One possibility is to simply forward requests for a mobile site to a different URL. Another possibility is to adapt the layout – or indeed content – programmatically as it’s generated on the server.
解决该问题的另一种方法是使用服务器端设备检测,然后根据该信息采取某些措施。 一种可能性是简单地将对移动站点的请求转发到其他URL。 另一种可能性是,在服务器上生成布局时,以编程方式调整布局,甚至调整内容。
Taking a server-side approach is the basis of this article, which looks in detail at the Browser Capabilities Project, or Browscap for short, to provide the information on which to base these decisions.
采用服务器端方法是本文的基础,本文将详细介绍“浏览器功能项目”(简称Browscap),以提供决策依据。
用户代理字符串简介 (An Introduction to User Agent Strings)
Before we can look at server-side detection, we need to understand the information available to the web application which comes in the form of user agent strings.
在查看服务器端检测之前,我们需要了解Web应用程序可用的信息,这些信息以用户代理字符串的形式出现。
When you make an HTTP request to retrieve a page from a web browser, it includes a piece of information called the user agent string. This provides information about who’s making the request and includes a variety of elements.
当您发出HTTP请求以从Web浏览器检索页面时,它包含一条称为用户代理字符串的信息 。 这提供了有关谁在发出请求的信息,并包括各种元素。
Typically the user agent string specifies what browser is used – it’s how analytics services can determine whether you’re using Chrome, Firefox, Safari, IE, or one of a multitude of other browsers. Furthermore, it can also provide information about the version of the requesting application. This is great for identifying the dwindling numbers of IE6 users, for example.
通常,用户代理字符串指定使用哪种浏览器–分析服务可以通过这种方式确定您使用的是Chrome,Firefox,Safari,IE还是其他多种浏览器之一。 此外,它还可以提供有关请求应用程序版本的信息。 例如,这对于识别IE6用户的数量减少非常有用。
Of course a browser is really just a specific type of application. Remember, HTTP requests aren’t just issued when your web browser accesses a web page or other resource. The request may be from a web crawler, a feed reader (ie: RSS), a validator, a library such as cURL, or one of a potentially infinite number of web service clients.
当然,浏览器实际上只是一种特定类型的应用程序。 请记住,当您的Web浏览器访问网页或其他资源时,不仅发出HTTP请求。 该请求可以来自Web爬网程序,提要阅读器(即RSS),验证器,库(例如cURL)或可能无限多个Web服务客户端之一。
The user agent string is also used to provide information about the operating system. Windows, Linux or Mac? iOS or Android? That, too is qualified with version numbers.
用户代理字符串还用于提供有关操作系统的信息。 Windows,Linux或Mac? iOS或Android? 那也符合版本号。
The user agent string will often also provide information about the physical device being used, whether it is a desktop, laptop, mobile, tablet, etc.
用户代理字符串通常还会提供有关正在使用的物理设备的信息,无论它是台式机,笔记本电脑,移动设备,平板电脑等。
Let’s look at some examples of user agent strings:
让我们看一些用户代理字符串的示例:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0
– This says that the request has been made by Firefox (version 24, to be specific) from an Intel-based Mac.
–这表示请求是由Firefox(具体来说是版本24)从基于Intel的Mac发出的。
Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25
Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25
– Safari on an iPad (running iOS6)
– iPad上的Safari(运行iOS6)
Opera/9.80 (J2ME/MIDP; Opera Mini/9.80 (S60; SymbOS; Opera Mobi/23.348; U; en) Presto/2.5.25 Version/10.54
Opera/9.80 (J2ME/MIDP; Opera Mini/9.80 (S60; SymbOS; Opera Mobi/23.348; U; en) Presto/2.5.25 Version/10.54
– Version 9 of Opera Mini on a Java-based mobile phone
–基于Java的手机上的Opera Mini版本9
Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
– Mobile Safari on an Android phone
– Android手机上的Mobile Safari
You can view hundreds of user agent strings online at useragentstring.com/pages/useragentstring.php.
您可以在useragentstring.com/pages/useragentstring.php上在线查看数百个用户代理字符串。
访问Browscap数据 (Accessing Browscap Data)
Considering the number of variables, the number of possible versions, and of course the ever-expanding range of physical devices being used, you’ll realize that the number of different permutations is quite large and growing every day. The Browser Capabilities Project is an attempt to catalogue these permutations. You can think of it of a database of user agent strings which associates them with some of the information which we can glean from them.
考虑到变量的数量,可能的版本的数量以及当然所使用的物理设备的范围不断扩大,您将意识到,不同排列的数量非常大,并且每天都在增加。 浏览器功能项目试图对这些排列进行分类。 您可以想到一个用户代理字符串数据库,该数据库将它们与我们可以从中收集的一些信息相关联。
In reality it’s not simply a database keyed by user agent string; it’s a series of rules and patterns used to extract relevant pieces of information, cross-referenced against known b