Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the maximum size of a cookie, and how many can be stored in a browser for each web site?

Tags:

cookies

I am learning about cookies, and I wonder about browser support when writing web applications that rely on cookies to store state.

  • For each domain/web site, how many cookies may be sent to a browser, and of what size?

  • If multiple cookies are sent and stored, does that affect performance?

like image 251
giri Avatar asked Jan 18 '10 15:01

giri


People also ask

What is the maximum size of a cookie?

cookies are usually limited to 4096 bytes and you can't store more than 20 cookies per site. By using a single cookie with subkeys, you use fewer of those 20 cookies that your site is allotted.

What is maximum number of cookies that can be stored in the website?

Chrome and Safari appear to allow more cookies per domain than Firefox, Opera or Internet Explorer. To be safe, it's best to stick with 30 to 50 maximum cookies per domain.

How long can cookies be stored in browser?

Persistent cookies remain on a computer indefinitely, although many include an expiration date and are automatically removed when that date is reached. Persistent cookies are used for two primary purposes: Authentication. These cookies track whether a user is logged in and under what name.


2 Answers

No more than 50 cookies per domain, with a maximum of 4 KB per cookie (or even 4 KB in total, see Iain's answer). On IE 6 it used to be 20 cookies per domain.

Generally it's recommended to preserve state on the server, and use cookies only for session tracking. They're sent along with every request, so they form an unnecessary overhead if the purpose is to keep session state around.

If you do want to keep state on the client, and you can use JavaScript to do it, there are options. Use the assorted storage API's directly or find a wrapper library that abstracts away the details.

Client-side storage options:

  • localStorage: Firefox 2+, Chrome 4+, Safari 4+, Internet Explorer 8+. 5 MB per domain without user confirmation (but be aware that it is stored as UTF-16 so you may use two bytes per character).
  • IndexedDB: Firefox 4+, Chrome 11+, Safari 10+, Internet Explorer 10+. 5 MB per domain without user confirmation, much more after confirmation (highly browser specific, check your browser for details).

Deprecated storage options:

  • Flash 8 persistent storage: any browser with Flash 8+. 100 KB, more with user permission. Deprecated because Flash itself is deprecated.
  • userData: Internet Explorer 5.5+. 64 KB per domain in the restricted zone, 128 KB per domain in the internet zone. Replaced by localStorage.
  • Web SQL: Chrome & Safari only, it will never make it to other browsers because it was not possible to standardize it.

So, generally for client-side storage it depends on the use case:

  • For session id tracking or for a few KB, use cookies.
  • Up to 2 MB, localstorage delivers a solution across all common browsers.
  • 2 MB and up, use IndexedDB (look for a good wrapper library).
like image 131
Joeri Sebrechts Avatar answered Oct 20 '22 07:10

Joeri Sebrechts


Cookie Size Limits

If you want to support most browsers, then do not exceed 50 cookies per domain, and 4093 bytes per domain. That is, the size of all cookies should not exceed 4093 bytes.

Performance Thoughts

Cookies are sent on every request for a domain, this includes images. For arguments sake, let's say you have 30 resources on your website, and have 4093 bytes of cookies. That means the user is uploading 122Kb of data. So if I have a 1Mbit upload connection, that will take at least 1 second.

If you want to see the cookie test page I created, or read more about it, check out Browser Cookie Limits.

like image 31
Iain Avatar answered Oct 20 '22 07:10

Iain