User-Agent

User-Agent 請求標頭(request header)含有能令網路協議同級層(peer)識別發出該用戶代理請求的軟體類型或版本號、該軟體使用的作業系統、還有軟體開發者的字詞串。

請讀讀透過用戶代理偵測瀏覽器以理解為什麼給不同的瀏覽器不同的頁面或服務是餿主意。

標頭類型 Request header
Forbidden header name

語法

User-Agent: <product> / <product-version> <comment>

瀏覽器常見格式:

User-Agent: Mozilla/5.0 (<system-information>) <platform> (<platform-details>) <extensions>

網路瀏覽器常用的格式:

User-Agent: Mozilla/<version> (<system-information>) <platform> (<platform-details>) <extensions>

指令

<product>
產品識別符:通常是名字或開發代號。
<product-version>
產品版本號。
<comment>
關於產品資訊的註解(如副產品訊息)。可能有、或沒有。

Firefox UA 字串

關於 Firefox 和基於 Gecko 的用戶代理字串,請參閱 Firefox 用戶代理字串參考。Firefox 用戶代理字串大略上分成以下四個部份:

Mozilla/5.0 (platform; rv:geckoversion) Gecko/geckotrail Firefox/firefoxversion

  1. Mozilla/5.0 is the general token that says the browser is Mozilla-compatible. For historical reasons, almost every browser today sends it.
  2. platform describes the native platform the browser is running on (Windows, Mac, Linux, Android, etc.), and if it's a mobile phone. Firefox OS phones simply say Mobile — the web is the platform. Note that platform can consist of multiple "; "-separated tokens. See below for further details and examples.
  3. rv:geckoversion indicates the release version of Gecko (such as "17.0"). In recent browsers, geckoversion is the same as firefoxversion.
  4. Gecko/geckotrail indicates that the browser is based on Gecko. (On Desktop, geckotrail is always the fixed string 20100101.)
  5. Firefox/firefoxversion indicates the browser is Firefox, and provides the version (such as "17.0").

示例

Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0
Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0

Chrome 用戶代理字串

Chrome(或基於 Chromium/blink 引擎的瀏覽器)的用戶代理字串看起來像 Firefox。出於相容性的理由,它還會加上「KHTML, like Gecko」與「Safari」的字串。

字串

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36

Opera 用戶代理字串

因為 Opera 瀏覽器的引擎也是基於 blink 的,所以語法也看起來也會很像。不過,還會加上「 OPR/<version>」一詞。

示例

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 OPR/38.0.2220.41

Opera 在使用 Presto 排版時的用戶代理字串

Opera/9.80 (Macintosh; Intel Mac OS X; U; en) Presto/2.2.15 Version/10.00
Opera/9.60 (Windows NT 6.0; U; en) Presto/2.1.1

Safari 用戶代理字串

此例的 safari 用戶代理字串是攜帶版,所以會出現「Mobile」一詞。

示例

Mozilla/5.0 (Linux; U; Android 4.0.3; de-ch; HTC Sensation Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30

Internet Explorer 用戶代理字串

示例

Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0)

網路爬蟲與機器人的用戶代理字串

示例

Googlebot/2.1 (+http://www.google.com/bot.html)

規範

規範 標題
RFC 7231, section 5.5.3: User-Agent Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content
RFC 2616, section 14.43: User-Agent Hypertext Transfer Protocol -- HTTP/1.1

瀏覽器相容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
User-AgentChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support

參閱