MDN wants to talk to developers like you: https://qsurvey.mozilla.com/s3/8d22564490d8

用戶代理

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

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

標頭類型 Request header
Forbidden header name

語法

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

網路瀏覽器常用的格式:

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

Directives

<product>
產品識別符
<product-version>
產品版本號
<comment>
關於產品資訊的註解。可能有、或沒有。

Firefox UA 字串

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

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

  • Mozilla/5.0 is the general token that says the browser is Mozilla compatible, and is common to almost every browser today.
  • platform describes the native platform the browser is running on (e.g. Windows, Mac, Linux or Android), and whether or not 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.
  • rv:geckoversion indicates the release version of Gecko (such as "17.0"). In recent browsers, geckoversion is the same as firefoxversion.
  • Gecko/geckotrail indicates that the browser is based on Gecko.
  • On Desktop, geckotrail is the fixed string "20100101"
  • 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

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

瀏覽器相容性

Feature Chrome Edge Firefox Internet Explorer Opera Safari Servo
User-Agent(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)
Feature Android Chrome for Android Edge Mobile Firefox for Android IE Mobile Opera Mobile Safari Mobile
User-Agent(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)

參閱

文件標籤與貢獻者

 此頁面的貢獻者: iigmir
 最近更新: iigmir,