previous | start | next

State Maintenance - Cookies

Whilst hidden fields are a simple technique for state maintenance, they are not a general solution. A cookie is a small piece of information which a server can store "within" a Web browser. For example, the following segment of Perl CGI code sends a cookie:
print "Content-type: text/html", "\n";
print "Set-cookie: MeLove=Cookie%20Monster", "\n\n"
print "<HTML">;.....rest of Web page
This stores "MeLove=Cookie Monster" with the browser. The following Perl CGI code reads a cookie:
print "Cookie:", $ENV{'HTTP_COOKIE'}, "\n";
Each cookie can have several extra attributes:
 
Name=Value
this attribute is compulsory, and more than one is allowed. Both "Name" and "Value" can be any ASCII string.
 
expires=DATE
defines the lifetime of the cookie. Default is the current browser session.
 
domain=DOMAIN
an Internet domain name to which this cookie may be sent.
 
path=PATH
defines the subset of URLs within a domain for which this cookie is valid and may be sent.

 


previous | start | next