![]() ![]() | ||
DOCUMENTATION - COOKIES
IntroductionThis document was written to support the Cookie Script written to evaluate Cookies and Cookie behaviour.
CookiesCookies, have now been around for some considerable time, and my involvement with them comes from a time around 1995 when Netscape Navigator was King, and sadly - to my mind - most of the Browsers of today lack the intuitive styling that these older Browsers possessed, and despite going forward, I think in many repects we have gone backwards regarding them, however, I digress ...Cookies, in many cases are still not very well understood. Many people have misconceptions, ranging from Cookies being Devilish or Dangerous or they are Viruses or Spyware, totally misunderstanding what they are, and how they work.
BackgroundLou Montulli an employee of Netscape Communications, is credited with applying the concept of - what was then known as - the 'Magic Cookie' to web communication in 1994. The Netscape Navigator - my favourite being 3 - supported cookies since its first version, but, Cookies are now supported by all major web browsers, and play a very important role as part of web development, without which today, many important web applications would be rendered useless. Should any further reading be required, a quick internet search will turn up the 'Original Cookie Specification' which has actually not changed very much since its inception!
What is a Cookie?A Cookie is quite simply a small text file that is stored by the Browser on the users machine - one of the reasons that struck fear into the heart of many a user! - they are formed of plain text, and contain no executable code whatsoever. A Web Page or Server instructs the Browser to store this information, which is looked at with each subsequent request, based on a set of rules. Web servers can then use this information to identify individual user requests, and as this is just text data, it isn't harmful in any respect, either in it's content, or of itself.BUT! - and it is quite a significant 'BUT' - they can be Classed as being very 'INTRUSIVE'
Cooke creationA web server specifies a cookie to be stored by sending an HTTP header calledSet-Cookie . The format of the Set-Cookie header is a simple string as follows:
The first part of the header is: 'The Value' which is typically a string in the format of a Name=Value Pair, such as that used in the Query String!
The options specified with
Server-Side frameworks normally provide functionality to parse cookies to make their values available.
The OptionsEach of the options after the cookie value are separated by a semicolon and space and each specifies rules about when the cookie should be sent back to the server, we will now consider each option in turn.
The - Expires OptionIndicates when the cookie should no longer be consider valid and sent to the server and therefore may be deleted by the browser. The value for this option is a date in the format:
Note: This is the standard JavaScript GMT/UTC Format which can be used directly, as in the 'JavaScript Cookie Script' such as:
Without the
The - Domain OptionThe next option isdomain , which indicates the domain(s) for which the cookie should be sent. By default, domain is set to the host name of the page setting the cookie, so the cookie value is sent whenever a request is made to the same host name. Eg. If the default domain for a cookie set on this site would be www.mjo.co.uk .
NB. The value set for the
The - Path OptionAnother way to control when theCookie header will be sent is to specify the path option. Similar to the domain option, path indicates a URL path that must exist in the requested resource before sending the Cookie header. This comparison is done by comparing the option value character-by-character against the start of the request URL. If the characters match, then the Cookie header is sent. For instance:
In this example, the
The - Secure OptionThe last option issecure . Unlike the other options, this is just a flag and has no additional value specified. A secure cookie will only be sent to the server when a request is made using SSL and the HTTPS protocol. The idea that the contents of the cookie are of high value and could be potentially damaging to transmit as clear text. Sample:
In reality, confidential or sensitive information should never be stored or transmitted in cookies as the entire mechanism is inherently insecure. By default, cookies set over an HTTPS connection are automatically set to be secure.
Cookie maintenance and lifecycleAny number of options can be specified for a single cookie, and those options may appear in any order. For example:
This cookie has three identifying characteristics: the cookie When a cookie is created with an expiration date, that expiration date relates to the cookie identified by it's full data. In order to change the expiration date of a cookie, you must specify the exact same data. When changing a cookies value, you need not set the expiration date each time because it's not part of the identifying information. For Example:
The expiration date of the cookie has now been set, so the next time I want to change the value of the cookie, I can just use its name, in fact, the expiration date won't change until it's manually changed. NB. Please be mindful that the expiration date is checked against the system time on the computer running the browser, there is no way to verify that the system time is in sync with server time, so errors may well occur when there is a discrepancy between the two!
Automatic Cookie RemovalThere are several reasons why a cookie might be automatically removed by the browser:
NB. Cookie management is important to avoid removal in cases where it it unintended, such as the above
Cookie restrictionsThere are restrictions placed on cookies in order to prevent abuse and protect both the browser and the server ie. The number of Cookies/Domain and Cookie size, and care must be observed in the case of the latter!
SubcookiesDue to the cookie number limit, developers have come up with the idea of subcookies to increase the amount of storage available to them. Subcookies are name-value pairs stored within a cookie value and typically have a format similar to below:
This approach allows a single cookie to hold multiple name/value pairs without going over the browsers Cookie limit. The downside to creating cookies in this format is that custom parsing is needed to extract the values rather than relying on the much simplier cookie format. Some server-side frameworks are beginning to support subcookie storage.
Cookies in JavaScriptAs can be seen from above, one of the reasons I wrote this article was to support the JavaScript Cookie Evaluation Script which can seen Here which can be used to fully investigate Cookie behavour in terms of Creation, Manipulation and the Removal of Cookies in JavaScript by using thedocument.cookie property. This property acts as the Set-Cookie header when assigned to, and as the Cookie header when read from. When creating a cookie, you must use a string that follows the same format that Set-Cookie expects:
Setting the value of
To retrieve cookie values in JavaScript, you just need to read from the Example:
Because of this, you need to parse the cookie string manually to extract actual cookie data. Inspection of the Script mentioned above will allow the user to fully investigate Cookie functionality!
Cookies returned by accessing
NB. The options as mentioned above are for the Browsers use only, so you cannot retrieve the
ConclusionThere is a great deal to know and understand regarding the creation and use of Cookies in order to use them effectively, and I do hope that this document, and the Evaluation Script, has provided at least some basic enlightenment regarding them! |
||
![]() |