我觉得这个问题以前必须在这里回答过,但我在任何地方都找不到。
网站能否检测到用户正在使用什么设备进行浏览?例如,如果用户正在浏览网站以查找应用程序,它是否知道根据他们使用的设备显示指向Google Play或Apple App Store的链接?
我认为$HTTP_USER_AGENT
这还不够,因为 iPad 用户可能正在使用Chrome,而 Android 用户可能正在使用Firefox等。
我可以使用 PHP,当然还有 Javascript。
The only way to detect the device or web browser it the User Agent String, Every web browser has to provide User Agent as per the HTTP 1.1 Protocol. The User Agent String can contain multiple product tokens. By convention, the product tokens are listed in order of their significance for identifying the application.
For browsers based on Mozilla, the user-agent string shall follow the format:
MozillaProductToken (MozillaComment) GeckoProductToken *
(VendorProductToken|VendorComment)
Applications that embed the Gecko layout engine shall have user-agent strings that follow the format:
ApplicationProductToken (ApplicationComment) GeckoProductToken *
(VendorProductToken|VendorComment)
Where:
ProductToken : Mozilla/ MozillaVersion
Version : Major . Minor
Comment : ( Platform; Security; OS-or-CPU;
Localization information;
GeckoVersion)*[; Optional Other Comments] )
Platform : Windows, Linux, Mac etc.
Security : N for no security;
U for strong security;
I for weak security
OS-or-CPU : Windows Version/MacOS
GeckoVersion : String starting with "rv:" followed
by the Gecko version
GeckoProductToken: Gecko/GeckoDate
GeckoDate : Date in the format YYYYMMDD
Examples
Mozilla Release
Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101
A branded release based on the same codebase as the browser above
Mozilla/5.001 (Macintosh; N; PPC; ja; rv:1.0) Gecko/25250101
MegaCorpBrowser/1.0 (MegaCorp, Inc.)
A re-branded release
Mozilla/9.876 (X11; U; Linux 2.2.12-20 i686, en; rv:2.0) Gecko/
25250101 Netscape/5.432b1 (C-MindSpring)
A Gecko-based browser
TinyBrowser/2.0 (TinyBrowser Comment; rv:1.9.1a2pre) Gecko/20201231
User String Specifications:
Opera/Version (OS-or-CPU; Encryption; Language)
They uses WebKit as its rendering engine but uses a different JavaScript engine. For Chrome’s initial beta release, version 0.2, the user-agent string carries along all of the information from WebKit as well as an extra section for the Chrome version. The format is as follows:
Mozilla/5.0 (Platform; Encryption; OS-or-CPU; Language)
AppleWebKit/AppleWebKitVersion (KHTML, like Gecko) Chrome/
ChromeVersion Safari/SafariVersion
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML,
like Gecko) Chrome/0.2.149.29 Safari/525.13
Konqueror, the browser bundled with the KDE Linux desktop environment, is based on the KHTML open-source rendering engine. Though available only on Linux, Konqueror has an active user base. For optimal compatibility, Konqueror opted to format its user-agent string after IE as follows:
Mozilla/5.0 (compatible; Konqueror/3.5; SunOS) KHTML/3.5.0 (like Gecko)
In 2003, Apple announced that it would release its own web browser, called Safari. The Safari rendering engine, called WebKit, began as a fork of the KHTML rendering engine used in the Linux-based Konqueror web browser. But how do you ensure that the browser isn’t locked out of popular sites? The answer is, put enough information into the user-agent string to convince web sites that the browser is compatible with another popular browser. This led to a user-agent string with the following format:
Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/
124 (KHTML, like Gecko) Safari/125.1
The platform part of the UA string indicates if Firefox is running on a phone-sized or tablet device. When Firefox runs on a device that has the phone form factor, there is a Mobile; token in the platform part of the UA string. For example:
Mozilla/5.0 (Android; Mobile; rv:13.0) Gecko/13.0 Firefox/13.0
Mozilla/5.0 (Android; Tablet; rv:13.0) Gecko/13.0 Firefox/13.0
However, if you use UA sniffing to target content to a device form factor, please look for Mobi (to include Opera Mobile, which uses "Mobi") for the phone form factor and do not assume any correlation between "Android" and the device form factor.
Android
Phone : Mozilla/5.0 (Android; Mobile; rv:13.0) Gecko/13.0 Firefox/13.0
Tablet : Mozilla/5.0 (Android; Tablet; rv:13.0) Gecko/13.0 Firefox/13.0
Firefox OS
Phone : Mozilla/5.0 (Mobile; rv:15.0) Gecko/15.0 Firefox/15.0
您可以同时使用服务器端和客户端技术来智能猜测访问者正在使用哪个设备:
虽然没有真正的保证(您基本上信任浏览器选择告诉您的任何信息),但这里有两个您可以了解用户设备的示例: