start page | rating of books | rating of authors | reviews | copyrights

Perl Cookbook

Perl CookbookSearch this book
Previous: 19.9. Debugging the Raw HTTP Exchange Chapter 19
CGI Programming
Next: 19.11. Creating Sticky Widgets
 

19.10. Managing Cookies

Problem

You want to get or set a cookie to help manage sessions or user preferences.

Solution

Using CGI.pm, retrieve an existing cookie like this:

$preference_value = cookie("preference name");

To prepare a cookie, do this:

$packed_cookie = cookie( -NAME    => "preference name",                          -VALUE   => "whatever you'd like",                          -EXPIRES => "+2y");

To save a cookie back to the client browser, you must include it in the HTTP header, probably using either the header or redirect functions:

print header(-COOKIE => $packed_cookie);

Discussion

Cookies store information on the client's browser. If you're using Netscape under Unix, you can inspect your own ~/.netscape/cookies file, although this doesn't show your current set of cookies. It only holds those cookies present when you last exited the browser. Think of them as per-application user preferences or a way to help with transactions. Benefits of cookies are that they can be shared between several different programs on your server, and they persist even across browser invocations.

However, cookies can be used for dubious tricks like traffic analysis and click tracing. This makes some folks very nervous about who is collecting their personal data and what use will be made of their page viewing habits. Cookies don't travel well, either. If you use a browser at home or in someone else's office, it won't have the cookies from the browser in your office. For this reason, do not expect every browser to accept the cookies you give it. As if that wasn't bad enough, browsers can randomly toss cookies. Here's an excerpt from the HTTP State Management Mechanism draft at http://portal.research.bell-labs.com/~dmk/cookie-2.81-3.1.txt :

Because user agents have finite space in which to store cookies, they may also discard older cookies to make space for newer ones, using, for example, a least-recently-used algorithm, along with constraints on the maximum number of cookies that each origin server may set.

Due to their unreliability, you should probably not place too much faith in cookies. Use them for simple, stateful transactions, and avoid traffic analysis for reasons of privacy.

Example 19.7 is a complete program that remembers the user's last choice.

Example 19.7: ic_cookies

#!/usr/bin/perl -w # 

ic_cookies - sample CGI script that uses a cookie use CGI qw(:standard);  use strict;  my $cookname = "favorite ice cream"; my $favorite = param("flavor"); my $tasty    = cookie($cookname) || 'mint';  unless ($favorite) {     print header(), start_html("Ice Cookies"), h1("Hello Ice Cream"),           hr(), start_form(),             p("Please select a flavor: ", textfield("flavor",$tasty)),               end_form(), hr();     exit; }  my $cookie = cookie(                 -NAME    => $cookname,                 -VALUE   => $favorite,                 -EXPIRES => "+2y",             );  print header(-COOKIE => $cookie),       start_html("Ice Cookies, #2"),       h1("Hello Ice Cream"),       p("You chose as your favorite flavor `$favorite'.");





See Also

The documentation for the standard CGI module


Previous: 19.9. Debugging the Raw HTTP Exchange Perl Cookbook Next: 19.11. Creating Sticky Widgets
19.9. Debugging the Raw HTTP Exchange Book Index 19.11. Creating Sticky Widgets

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.