|
||||||
How to Use Cookies with a Perl CGI Web PageCreating and Reading Cookies using the Perl Programming LanguageCookies 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:
And, for the Perl programmer, they're very easy to work with. Setting the HTTP Header with PerlAll 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 PerlThe 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:
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:
However, once the cookie has been sent with the header then it can be read by the Perl script. Reading Cookies with PerlThe 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 PerlIf 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.
|
||||||
|
|
||||||
|
|
||||||