Web浏览器的Cookie**的最大大小是多少?

本文翻译自:What is the maximum size of a web browser's cookie's key?

What is the maximum size of a web browser's cookie's key? Web浏览器的Cookie**的最大大小是多少?

I know the maximum size of a cookie is 4KB, but does the key have a limitation as well? 我知道Cookie的最大大小为4KB,但是**也有限制吗?


#1楼

参考:https://stackoom.com/question/2GJI/Web浏览器的Cookie**的最大大小是多少


#2楼

You can also use web storage too if the app specs allows you that (it has support for IE8+). 如果应用规范允许,您也可以使用Web存储(它支持IE8 +)。

It has 5M (most browsers) or 10M (IE) of memory at its disposal. 它拥有5M (大多数浏览器)或10M (IE) 可用的内存。

" Web Storage (Second Edition) " is the API and " HTML5 Local Storage " is a quick start. Web存储(第二版) ”是API,“ HTML5本地存储 ”是快速入门。


#3楼

After testing a few browsers myself, and using Browser Shots I have compiled the following list 在自己测试了几个浏览器并使用Browser Shots之后,我编译了以下列表

Web浏览器的Cookie**的最大大小是多少?


#4楼

The 4K limit you read about is for the entire cookie, including name, value, expiry date etc. If you want to support most browsers, I suggest keeping the name under 4000 bytes, and the overall cookie size under 4093 bytes. 您所了解的4K限制适用于整个Cookie,包括名称,值,有效期等。如果要支持大多数浏览器,建议将名称保留在4000字节以下,并将Cookie的整体大小保留在4093字节以下。

One thing to be careful of: if the name is too big you cannot delete the cookie (at least in JavaScript). 要注意的一件事:如果名称太大,则无法删除cookie(至少在JavaScript中)。 A cookie is deleted by updating it and setting it to expire. 通过更新cookie并将其设置为过期来删除cookie。 If the name is too big, say 4090 bytes, I found that I could not set an expiry date. 如果名称太大(例如4090字节),我发现无法设置有效期限。 I only looked into this out of interest, not that I plan to have a name that big. 我只是出于兴趣而调查了此内容,而不是打算计划这么大的名字。

To read more about it, here are the " Browser Cookie Limits " for common browsers. 要了解更多信息,这里是普通浏览器的“ 浏览器Cookie限制 ”。


While on the subject, if you want to support most browsers, then do not exceed 50 cookies per domain , and 4093 bytes per domain . 在此主题上,如果您想支持大多数浏览器,则每个域的Cookie不能超过50 个,每个域不能超过4093个字节 That is, the size of all cookies should not exceed 4093 bytes. 也就是说,所有cookie的大小不应超过4093字节。

This means you can have 1 cookie of 4093 bytes, or 2 cookies of 2045 bytes, etc. 这意味着您可以拥有1个4093字节的cookie,或2个2045字节的cookie,等等。


I used to say 4095 bytes due to IE7, however now Mobile Safari comes in with 4096 bytes with a 3 byte overhead per cookie, so 4093 bytes max. 由于IE7,我曾经说4095个字节,但是现在Mobile Safari附带4096个字节,每个cookie的开销为3个字节,因此最大为4093个字节。


#5楼

A cookie key(used to identify a session) and a cookie are the same thing being used in different ways. cookie键(用于标识会话)和cookie是同一事物,只是使用方式不同。 So the limit would be the same. 因此限制将是相同的。 According to Microsoft its 4096 bytes. 根据Microsoft的4096字节。

MSDN MSDN

cookies are usually limited to 4096 bytes and you can't store more than 20 cookies per site. Cookie通常限制为4096个字节,并且每个站点最多只能存储20个Cookie。 By using a single cookie with subkeys, you use fewer of those 20 cookies that your site is allotted. 通过将单个Cookie与子键一起使用,您可以在分配给网站的那20个Cookie中使用较少的cookie。 In addition, a single cookie takes up about 50 characters for overhead (expiration information, and so on), plus the length of the value that you store in it, all of which counts toward the 4096-byte limit. 此外,单个cookie会占用约50个字符的开销(到期信息等),以及您存储在其中的值的长度,所有这些都计入4096字节的限制。 If you store five subkeys instead of five separate cookies, you save the overhead of the separate cookies and can save around 200 bytes. 如果存储五个子项而不是五个单独的cookie,则可以节省单独的cookie的开销,并且可以节省大约200个字节。


#6楼

Actually, RFC 2965, the document that defines how cookies work, specifies that there should be no maximum length of a cookie's key or value size , and encourages implementations to support arbitrarily large cookies. 实际上,RFC 2965是定义cookie的工作方式的文档,它指定cookie的键或值的大小不应超过最大长度 ,并鼓励实现支持任意大的 cookie。 Each browser's implementation maximum will necessarily be different, so consult individual browser documentation. 每个浏览器的实现最大值一定会有所不同,因此请查阅各个浏览器文档。

See section 5.3, "Implementation Limits", in the RFC . 请参阅RFC第5.3节“实施限制”。