Previous   -   Index   -   Next  >


What are Cookies?


Cookies store a small amount of data on the client machine.

A cookie is a small amount of named data stored by a web browser and associated with a particular web page or web site or domain (and user profile). Ordinarily used to store client data on the initial visit to a site (e.g. name, password, preferences) so that the data can be used on subsequent visits (state preservation).

Cookie data can be read and written by the server but the data is stored on the client machine. The cookie is passed from the client to the server in the HTTP header of all HTTP requests for which the client has a cookie. The server does not request the cookie, the client sends the cookie to the server whether it wants it or not. The server may send a cookie to the client when servicing a HTTP request but the client may refuse to accept the cookie (why?).

JavaScript provides client side access to cookie data, but then the JavaScript that manipulates the cookie on the client also comes from the server. JavaScript is a means by which the server can manipulate the cookie on the client machine.

Cookies were introduced for the express purpose of maintaining state in an HTTP session, which is good but:
Documents containing cookies are personalised and so cannot be cached by proxy servers.
Cookies are transmitted in a readable text form so personalised information is visible.

Cookies are available to CGI programs through the HTTP_COOKIE environment variable. Each cookie has a name and its associated data is stored as a name=value pair where value is a string.

Limitations (RFC2109,sec. 6.3), a web client should be capable of storing:

To use cookies efficiently infornmation should be packed. Efficient cookie use should pack information, for example...

   mailinfo=Title&Mr&Surname&Smith&Initial&J&Email&j.smith@gre.ac.uk

...in this example Title="Mr", Surname="Smith" and so on.


Mail Form Using Cookies

mailform3.pl

#!/usr/local/bin/perl

# mailform3.pl - k.mcmanus@gre.ac.uk 20001028:20011010:20021107

# CGI program: generates a mailing list form using a cookie

use CGI qw(:standard);

%details = cookie('mailinfo');
%oslabels = (
   Win9x  => "Windoze 95/98/ME\n",
   WinNT  => "Windoze NT\n",
   Win2k  => "Windoze 2000\n",
   WinXP  => "Windoze XP\n",
   Unix   => "Unix\n",
   MacOS  => "Mac OS\n",
   MacOSX => "Mac OS X\n",
   VMS    => "Open VMS\n"
);
@osvalues = sort keys %oslabels;

print
   header(-type => 'text/html',
          -expires => 'now',
          -charset => 'UTF-8'),
   start_html(-title => 'Mailing list form',
              -style => {-src => '../comp1037/perl/mailform.css'}),
   h2('Join our product mailing list'), "\n",
   h4('(CGI.pm cookie)'),
   start_form(-action => 'maillist2.pl'),
      "\n Title ", textfield('Title', "$details{'Title'}", 8, 16),
      "\n Initials ", textfield('Initials', "$details{'Initials'}", 6, 12),
      "\n Surname ", textfield('Surname', "$details{'Surname'}", 32, 64), br, br,
      "\n Email ", textfield('Email', "$details{'Email'}", 32, 64), br, br,
      "\n Your computer platform(s):\n", br,
      checkbox_group(-name => 'Platforms',
                     -values =>  \@osvalues,
                     -defaults => [$details{'Platforms'}],
                     -linebreak => 'true',
                     -labels => \%oslabels), br, br, "\n",
      submit('Submit', 'With Cookie'), " \n",
      submit('Submit', 'Without Cookie'), " \n",
      reset('Reset Form'),
      hidden(-name => 'HiddenName', -default => 'mailform3.pl'),
      end_form, "\n",
   hr, "\n",
   a({-href=>$ENV{HTTP_REFERER}}, 'Back to the cookie notes'),
   hr, end_html;

Things to note about mailform3.pl


maillist2.pl

#!/usr/local/bin/perl

# maillist2.pl - k.mcmanus@gre.ac.uk 20001028,20021108

# Mailing list example CGI program:
#     accepts data from a form
#     logs it to file for later processing by a separate batch program
#     returns confirmation of the data to the user
#     offers state logging in a cookie

use lib ('/home/mk05/perl/lib');
use MyTime qw(dtstamp);
use CGI qw(:standard);
use Fcntl qw(:flock);

# Save the data into mlist.txt

open(MLISTHANDLE, ">> ../comp1037/perl/mlist.txt");
flock(MLISTHANDLE, LOCK_EX);
print MLISTHANDLE dtstamp('compact'), ":", param('Email'), ":",
   param('Title')," ", param('Initials'), " ", param('Surname'), ":";
foreach $val(param('Platforms')) { print MLISTHANDLE $val, " " }
print MLISTHANDLE "\n";
flock(MLISTHANDLE, LOCK_UN);
close(MLISTHANDLE);

# Handle cookie

foreach (param) {
   $details{$_} = param($_);
}
if (param('Submit') eq 'With Cookie') {
   $mlistcookie = cookie(-name    => 'mailinfo',
                         -value   => \%details,
                         -expires => '+1h');
} elsif (param('Submit') eq 'Without Cookie') {
   $mlistcookie = cookie(-name    => 'mailinfo',
                         -value   => \%details,
                         -expires => '-1h');
}

# Return confirmation

print
   header(-type    => 'text/html',
          -expires => 'now',
          -cookie  => $mlistcookie,
          -charset => 'UTF-8'),
   start_html(-title => 'Results from mlist2.pl',
              -style => {-src => '../comp1037/perl/mailform.css'}),
   h2('The following details about you are now held on our mailing list:'),
   "\n Title: ", param('Title'), br,
   "\n Initials: ", param('Initials'), br,
   "\n Surname: ", param('Surname'), br,
   "\n Email: ", param('Email'), br, br,
   "\n Platforms: ", br;
foreach $val (param('Platforms')) { print "       ", $val, br }
print
   hr, "\n", a({-href => "$ENV{HTTP_REFERER}"}, "Return to the previous page"),
   br, "\n", a({-href => "../comp1037/perl/mlist.txt"}, "Look at the mailing list"),
   hr, end_html;

Things to notice about the cookie code in maillist2.pl:


Previous   -   Index   -   Next  >

best viewed using Mozilla browsers
© k.mcmanus 2004
Valid XHTML 1.0! . Valid CSS . WCAG priority 3 approved