How to Use Cookies with a Perl CGI Web Page

Creating and Reading Cookies using the Perl Programming Language

© Mark Alexander Bain

Jul 19, 2009
How to Use Cookies with a Perl CGI Web Page, Mark Alexander Bain
Cookies are small pieces of information left on a computer by a web page, and they can be manipulated by any Perl programmer.

Cookies are used in many web sites. They're pieces of information left on the the user's computer by the web browser. They tend to be used to:

  • keep count of the number of times that a user has accessed a web page
  • store user details
  • record whether or not the user wants to remain logged on to a web site when they close the web browser

And, for the Perl programmer, they're very easy to work with.

Setting the HTTP Header with Perl

All Perl CGI scripts start with a shebang line. This tells the web server about the location of the perl executable:

#!c:/strawberry/perl/bin/perl

The script must then set the HTTP headers.These contain information that the browser require although normally the Perl programmer is not interested in the actual details and would simply use the CGI module's header method:

use CGI qw/:standard/;
print header;

However, it's also this header method that is used to set the cookies.

Setting Cookies with Perl

The cookies themselves accept an associative array (or hash) as an input, for example:

%site_cookie = (
'firstname' => 'Fred',
'surname' => 'Smith'
);

This is then used with the cookie method:

$cookie = cookie(
-name => 'site_cookie',
-value => \%site_cookie,
-expires => '+365d'
);

The parameters used here by the cookie method are:

  • -name - the name of the cookie
  • -value - the associative array
  • -expires - the amount of time that the cookie will remain on the computer (if this is not set then the cookie will expire as soon as the web browser is closed)

Finally the cookie is sent to the browser as part of the header.

print header (-cookie => $cookie);

It is important for the programmer to remember that:

  • the header may only be sent once
  • nothing is allowed to be output to the web page before the header is sent

However, once the cookie has been sent with the header then it can be read by the Perl script.

Reading Cookies with Perl

The cookie is read into an associative array by using the cookie method again, but this time only using the name parameter:

%cookie_values = cookie(-name => 'site_cookie');

The contents of the cookie can then be displayed in the web browser:

while (($key, $value) = each(%cookie_values)) {
print "$key = $value ";
}

However, much more interesting things can be done with the cookie than just displaying its contents.

Keeping a Count with Cookies and Perl

If the contents of a cookie are to be used in the programming then the first step must be to read them into an array:

#!c:/strawberry/perl/bin/perl
%cookie_values = cookie(-name => 'site_cookie');

In this example the cookie will keep count of the number of times that a user accesses the web page. Therefore, the hash is checked to see if the count has already been set and updates it accordingly:

if ($cookie_values{'access_count'}) {
$cookie_values{'access_count'} = $cookie_values{'access_count'} + 1
} else {
$cookie_values{'access_count'} = 1;
}

It is important that no outputs are sent to the screen until the header has been sent, and so that's done next:

%site_cookie = (
'firstname' => 'Fred', 'surname' => 'Smith',
'access_count' => $cookie_values{'access_count'}
);
$cookie = cookie(-name => 'site_cookie', -value => \%site_cookie, -expires => '+365d');
print header (-cookie => $cookie);

Once the header is sent then the results of reading and updating the cookie can be displayed to the screen:

print "You've accessed this page $cookie_values{'access_count'} times ";

And so, at the end of this process, the Perl programmer will have a CGI application that will save cookies to the user's computer. The application will also be able to read those cookies. The programmer, therefore, can store any relevant information that their application requires.


The copyright of the article How to Use Cookies with a Perl CGI Web Page in Computer Programming Tutorials is owned by Mark Alexander Bain. Permission to republish How to Use Cookies with a Perl CGI Web Page in print or online must be granted by the author in writing.


How to Use Cookies with a Perl CGI Web Page, Mark Alexander Bain
Figure 1: A Perl CGI Cookie Application, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo