Play with Cookies

[ Send a cookie from a client ]

Before we will go to the Perl style of Cookie handling it might be useful to get know how Cookies are delivered from a webserver in common:

Set-Cookie: foo=foobar; path=/; expires=Thu, 24-Jun-2004 21:13:36 GMT
Set-Cookie: bar=barfoo; path=/; expires=Sun, 22-Jun-2014 09:13:36 GMT
...

To send cookies from a client to the server just omit the "Set-" prefix. Cookies are transferred within the HTTP Header block. A http header and the related http content are being separated with two following new lines (\n\n). A server-side Perl script stores all the received Cookies in the $ENV{HTTP_COOKIE} environment variable as well, multiple Cookies are separated by comma.

Values above ASCII code 127 should be URI encoded, e.g. "%20" for a space character.

Other typical http header keys and its values sent from a webserver to the client are
Server: Apache/1.3.29
Content-Type: text/html; charset=ISO-8859-1
.

--reto

my $query = CGI->new;

# read cookie 'your_cookie_name':
my %cookie_val = $query->cookie(-name => 'your_cookie_name');

# Debug: foreach (keys %cookie_val) { Debug "----> $_: $cookie_val{$_}" }


print $query->header(-type=>'image/gif',
                     -nph=>1,
                     -status=>'402 Payment required',
                     -expires=>'+3d',
                     -cookie=>$cookie,
                     -Cost=>'$2.00');

The -nph parameter, if set to a true value, will issue the correct headers to work with a NPH (no-parse-header) script. This is important to use with certain servers, such as Microsoft Internet Explorer, which expect all their scripts to be NPH.


expires

When you specify an absolute or relative expiration interval with this parameter, some browsers and proxy servers will cache the script's output until the indicated expiration date. The following forms are all valid for the -expires field:

               +30s                              30 seconds from now
               +10m                              ten minutes from now
               +1h                               one hour from now
               -1d                               yesterday (i.e. "ASAP!")
               now                               immediately
               +3M                               in three months
               +10y                              in ten years time
               Thursday, 25-Apr-1999 00:40:33 GMT  at the indicated time & date

Store a cookie


 my $cookie = $q->cookie (
                -name    => 'wtk_cust_logon',
                -value   => \%cookie_val,
                -path    => '/',
                -expires => '+12h'
 );

 my $cookie_persist = $q->cookie (
                -name    => 'wtk_cust_logon_persist',
                -value   => \%cookie_val_persist,
                -path    => '/',
                -expires => '+3M' # 3 months in the future
 );

 # store cookie(s)
 print $q->header(-cookie => [$cookie, $cookie_persist]);

Delete a cookie

 # delete cookie....
 #
 my $cookie = $q->cookie (
                -name    => 'wtk_cust_logon',
                -value   => '',
                -path    => '/',
                -expires => '-1d'
 );

 print $q->header(-cookie => $cookie);

 # default content-type text/html is set as well from now



Further References

  1. perl/cgi-lite.htm (Perl)
    http://www.infocopter.com/perl/cgi-lite.htm
    Perl module to process and decode WWW forms and cookies (2004-06-24 10:31)

  2. perl/cookie.htm (Perl)
    http://www.infocopter.com/perl/cookie.htm
    Cookie Handling with Perl (2004-06-24 10:17)

  3. Perldoc.com/perl5.8.0/lib/lwpcook.html (Perl)
    http://www.perldoc.com/perl5.8.0/lib/lwpcook.html
    Examples that show typical usage of the libwww-perl library (2004-06-23 17:41)


copyright by reto - created with mytexi