04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 19: Client - Side Storage<br />

This extra information being sent back to the server can be used to uniquely identify the client from<br />

which the request was sent.<br />

Restrictions<br />

Cookies are, by nature, tied to a specific domain. When a cookie is set, it is sent along with requests to<br />

the same domain from which it was created. This restriction ensures that information stored in cookies is<br />

available only to approved recipients and cannot be accessed by other domains.<br />

Since cookies are stored on the client computer, restrictions have been put in place to ensure that cookies<br />

can ’ t be used maliciously and that they won ’ t take up too much disk space. The total number of<br />

cookies per domain is limited, although it varies from browser to browser. For example:<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

Internet Explorer (IE) 6 and lower enforced a limit of 20 cookies per domain.<br />

IE 7 and later have a limit of 50 cookies per domain. IE 7 initially shipped with support for a<br />

maximum of 20 cookies per domain, but that was later updated with a patch from Microsoft.<br />

Firefox limits cookies to 50 per domain.<br />

Opera limits cookies to 30 per domain.<br />

Safari and Chrome have no hard limit on the number of cookies per domain.<br />

When cookies are set above the per - domain limit, the browser starts to eliminate previously set cookies.<br />

IE and Opera begin by removing the least recently used (LRU) cookie to allow space for the newly set<br />

cookie. Firefox seemingly randomly decides which cookies to eliminate, so it ’ s very important to mind<br />

the cookie limit to avoid unintended consequences.<br />

There are also limitations as to the size of cookies in browsers. Most browsers have a byte-count limit of<br />

around 4096 bytes, give or take a byte. For best cross - browser compatibility, it ’ s best to keep the total<br />

cookie size to 4095 bytes or less. The size limit applies to all cookies for a domain, not per cookie.<br />

If you attempt to create a cookie that exceeds the maximum cookie size, the cookie is silently dropped.<br />

Note that one character typically takes one byte, unless you ’ re using multibyte characters.<br />

Cookie Parts<br />

Cookies are made up of the following pieces of information stored by the browser:<br />

❑<br />

❑<br />

❑<br />

Name — A unique name to identify the cookie. Cookie names are case - insensitive, so myCookie<br />

and MyCookie are considered to be the same. In practice, however, it ’ s always best to treat the<br />

cookie names as case - sensitive because some server software may treat them as such. The cookie<br />

name must be URL - encoded.<br />

Value — The string value stored in the cookie. This value must also be URL - encoded.<br />

Domain — The domain for which the cookie is valid. All requests sent from a resource at this<br />

domain will include the cookie information. This value can include a subdomain (such as<br />

www.wrox.com ) or exclude it (such as .wrox.com , which is valid for all subdomains of wrox<br />

.com ). If not explicitly set, the domain is assumed to be the one from which the cookie was set.<br />

618

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!