Chapter 7. CGI In Use
CGI In Use
Presenting data from a database
Uploading files to the server
Requesting info from other servers
The CGI module makes setting and retrieving cookies very easy. There are special "cookie" functions that allow you to build a cookie header, and retrieve cookie values.
Basically, there are two functions; one for reading cookies
#Read the cookie variable
$ID = $cgi->cookie('ID');
and one for writing cookies
$ID_cookie = $cgi->cookie( -name => ID, -value => $name,
-path => '/', -expires => '+1M' );
The function to create a cookie returns a string, in this case saved in $ID_cookie, which is then sent out as part of the header to set the cookie at the browser:
print $cgi->header( -cookie => $ID_cookie );
To see this in action, bring up a small cookie handling form here, and try out the Set, Check and Clear buttons.
Next time you send any form in to the server, the cookie goes along for the ride. This cookie is available to any script on the server from then on.
Click here to look at the perl script that handles these cookies.
Email me your comments!