Cookies & Perl

Cookies are a mechanism to save state information in a browser. A web application can send one or more cookies to the browser accessing the application. The browser will then bring the cookie(s) back to the application on subsequent accesses, until the cookie(s) expires. Each cookie has a name and a value (similar to named parameters in the QUERY_STRING). This mini tutorial shows you how to:

  1. Create and send cookies
  2. Retrieve cookies
  3. Create persistent cookies

Creating and sending cookies

Use the method called cookie to create a cookie. Assuming that you've created a CGI $query object using $query = new CGI; here is how you would create a cookie whose name is firstname and value is krish:

$firstcookie = $query->cookie( {-name => 'firstname', -value => 'krish'});

Although this creates the cookie, it is not sent to the browser unless you explicitly do so. Cookies must be sent in the HTTP response header generated by your application. You can use the following statement to send the above cookie to the brower:

print $query->header( { -cookie => $firstcookie, -type => "text/html" } );

(Note: As always you should ensure that the header is printed before any other text.)

If you have multiple cookies, create all of them and send them in the header as a list. Here is an example:

$firstcookie = $query->cookie( {-name => 'firstname', -value => 'krish'});
$nextcookie = $query->cookie( {-name => 'lastname', -value => 'pillaipakkam'});
print $query->header( {-cookie => [$firstcookie, $nextcookie]} );

If you have an array of cookies, make sure you send a reference to the array of cookies in the header method. For example, if you have @mycookies as an array of cookies, here is how you would send all of them in the header:

print $query->header( {-cookie => \@mycookies } );

You can also modify a cookie value the same way you set a cookie. For example, to change the value of the cookie whose name is firstname, the following works:

#new value for firstname
$firstcookie = $query->cookie( {-name => 'firstname', -value => 'krishnan'});

Retrieving cookies

When a browser brings back a cookie that has previously been set you can find its value using the cookie method again. Just don't set the value of the cookie. Here is how you would retrieve the cookie named firstname:

$myvar = $query->cookie({-name => 'firstname'));

If the cookie named firstname does not exist, then $myvar will be undef. You can therefore check for the existence of the cookie using an if statement like this:

if ( ! $myvar ) { #Cookie was not set
# Do whatever needs to be done
}

Session cookies vs persistent cookies

By default new cookies are session cookies. That is, these cookies expire whenever the all browser windows on the client are closed. If you want a cookie to survive beyond this you need to make them persistent. This involves simply setting the expiry time of a cookie. For example, if you want to cookie to persist until January 1, 2010, you would perhaps try something like this:

$query->cookie({-name=>'l',-value=>'c', -expires =>'1-Jan-2010 00:00:00 GMT'});

You can also use shortcuts like:

#Cookie expires 10 seconds from now
$query->cookie({-name=>'l',-value=>'c', -expires =>'+10s'});
#Cookie expires 10 minutes from now
$query->cookie({-name=>'l',-value=>'c', -expires =>'+10m'});
#Cookie expires 10 hours from now
$query->cookie({-name=>'l',-value=>'c', -expires =>'+10h'});
#Cookie expires 10 days from now
$query->cookie({-name=>'l',-value=>'c', -expires =>'+10d'});
#Cookie expires 10 months from now
$query->cookie({-name=>'l',-value=>'c', -expires =>'+10M'});
#Cookie expires 10 years from now
$query->cookie({-name=>'l',-value=>'c', -expires =>'+10y'});

Use negative to go back in time (to make a cookie expire immediately).

You might want to look at a simple CGI perl program that demonstrates cookies.