1Simple::Cookie(3)     User Contributed Perl Documentation    Simple::Cookie(3)
2
3
4

NAME

6       CGI::Cookie - Interface to Netscape Cookies
7

SYNOPSIS

9           use CGI::Simple::Standard qw(header);
10           use CGI::Simple::Cookie;
11
12           # Create new cookies and send them
13           $cookie1 = new CGI::Simple::Cookie( -name=>'ID', -value=>123456 );
14           $cookie2 = new CGI::Simple::Cookie( -name=>'preferences',
15                                               -value=>{ font => Helvetica,
16                                                         size => 12 }
17                                             );
18           print header( -cookie=>[$cookie1,$cookie2] );
19
20           # fetch existing cookies
21           %cookies = fetch CGI::Simple::Cookie;
22           $id = $cookies{'ID'}->value;
23
24           # create cookies returned from an external source
25           %cookies = parse CGI::Simple::Cookie($ENV{COOKIE});
26

DESCRIPTION

28       CGI::Simple::Cookie is an interface to Netscape (HTTP/1.1) cookies, an
29       innovation that allows Web servers to store persistent information on
30       the browser's side of the connection.  Although CGI::Simple::Cookie is
31       intended to be used in conjunction with CGI::Simple.pm (and is in fact
32       used by it internally), you can use this module independently.
33
34       For full information on cookies see:
35
36           http://www.ics.uci.edu/pub/ietf/http/rfc2109.txt
37

USING CGI::Simple::Cookie

39       CGI::Simple::Cookie is object oriented.  Each cookie object has a name
40       and a value.  The name is any scalar value.  The value is any scalar or
41       array value (associative arrays are also allowed).  Cookies also have
42       several optional attributes, including:
43
44       1. expiration date
45           The expiration date tells the browser how long to hang on to the
46           cookie.  If the cookie specifies an expiration date in the future,
47           the browser will store the cookie information in a disk file and
48           return it to the server every time the user reconnects (until the
49           expiration date is reached).  If the cookie species an expiration
50           date in the past, the browser will remove the cookie from the disk
51           file.  If the expiration date is not specified, the cookie will
52           persist only until the user quits the browser.
53
54       2. domain
55           This is a partial or complete domain name for which the cookie is
56           valid.  The browser will return the cookie to any host that matches
57           the partial domain name.  For example, if you specify a domain name
58           of ".capricorn.com", then Netscape will return the cookie to Web
59           servers running on any of the machines "www.capricorn.com",
60           "ftp.capricorn.com", "feckless.capricorn.com", etc.  Domain names
61           must contain at least two periods to prevent attempts to match on
62           top level domains like ".edu".  If no domain is specified, then the
63           browser will only return the cookie to servers on the host the
64           cookie originated from.
65
66       3. path
67           If you provide a cookie path attribute, the browser will check it
68           against your script's URL before returning the cookie.  For exam‐
69           ple, if you specify the path "/cgi-bin", then the cookie will be
70           returned to each of the scripts "/cgi-bin/tally.pl",
71           "/cgi-bin/order.pl", and "/cgi-bin/customer_service/complain.pl",
72           but not to the script "/cgi-private/site_admin.pl".  By default,
73           the path is set to "/", so that all scripts at your site will
74           receive the cookie.
75
76       4. secure flag
77           If the "secure" attribute is set, the cookie will only be sent to
78           your script if the CGI request is occurring on a secure channel,
79           such as SSL.
80
81       Creating New Cookies
82
83           $c = new CGI::Simple::Cookie( -name    =>  'foo',
84                                         -value   =>  'bar',
85                                         -expires =>  '+3M',
86                                         -domain  =>  '.capricorn.com',
87                                         -path    =>  '/cgi-bin/database',
88                                         -secure  =>  1
89                                       );
90
91       Create cookies from scratch with the new method.  The -name and -value
92       parameters are required.  The name must be a scalar value.  The value
93       can be a scalar, an array reference, or a hash reference.  (At some
94       point in the future cookies will support one of the Perl object serial‐
95       ization protocols for full generality).
96
97       -expires accepts any of the relative or absolute date formats recog‐
98       nized by CGI::Simple.pm, for example "+3M" for three months in the
99       future.  See CGI::Simple.pm's documentation for details.
100
101       -domain points to a domain name or to a fully qualified host name.  If
102       not specified, the cookie will be returned only to the Web server that
103       created it.
104
105       -path points to a partial URL on the current server.  The cookie will
106       be returned to all URLs beginning with the specified path.  If not
107       specified, it defaults to '/', which returns the cookie to all pages at
108       your site.
109
110       -secure if set to a true value instructs the browser to return the
111       cookie only when a cryptographic protocol is in use.
112
113       Sending the Cookie to the Browser
114
115       Within a CGI script you can send a cookie to the browser by creating
116       one or more Set-Cookie: fields in the HTTP header.  Here is a typical
117       sequence:
118
119           $c = new CGI::Simple::Cookie( -name    =>  'foo',
120                                          -value   =>  ['bar','baz'],
121                                          -expires =>  '+3M'
122                                         );
123
124           print "Set-Cookie: $c\n";
125           print "Content-Type: text/html\n\n";
126
127       To send more than one cookie, create several Set-Cookie: fields.
128       Alternatively, you may concatenate the cookies together with "; " and
129       send them in one field.
130
131       If you are using CGI::Simple.pm, you send cookies by providing a
132       -cookie argument to the header() method:
133
134         print header( -cookie=>$c );
135
136       Mod_perl users can set cookies using the request object's header_out()
137       method:
138
139         $r->header_out('Set-Cookie',$c);
140
141       Internally, Cookie overloads the "" operator to call its as_string()
142       method when incorporated into the HTTP header.  as_string() turns the
143       Cookie's internal representation into an RFC-compliant text representa‐
144       tion.  You may call as_string() yourself if you prefer:
145
146           print "Set-Cookie: ",$c->as_string,"\n";
147
148       Recovering Previous Cookies
149
150           %cookies = fetch CGI::Simple::Cookie;
151
152       fetch returns an associative array consisting of all cookies returned
153       by the browser.  The keys of the array are the cookie names.  You can
154       iterate through the cookies this way:
155
156           %cookies = fetch CGI::Simple::Cookie;
157           foreach (keys %cookies) {
158               do_something($cookies{$_});
159           }
160
161       In a scalar context, fetch() returns a hash reference, which may be
162       more efficient if you are manipulating multiple cookies.
163
164       CGI::Simple.pm uses the URL escaping methods to save and restore
165       reserved characters in its cookies.  If you are trying to retrieve a
166       cookie set by a foreign server, this escaping method may trip you up.
167       Use raw_fetch() instead, which has the same semantics as fetch(), but
168       performs no unescaping.
169
170       You may also retrieve cookies that were stored in some external form
171       using the parse() class method:
172
173              $COOKIES = `cat /usr/tmp/Cookie_stash`;
174              %cookies = parse CGI::Simple::Cookie($COOKIES);
175
176       Manipulating Cookies
177
178       Cookie objects have a series of accessor methods to get and set cookie
179       attributes.  Each accessor has a similar syntax.  Called without argu‐
180       ments, the accessor returns the current value of the attribute.  Called
181       with an argument, the accessor changes the attribute and returns its
182       new value.
183
184       name()
185           Get or set the cookie's name.  Example:
186
187               $name = $c->name;
188               $new_name = $c->name('fred');
189
190       value()
191           Get or set the cookie's value.  Example:
192
193               $value = $c->value;
194               @new_value = $c->value(['a','b','c','d']);
195
196           value() is context sensitive.  In a list context it will return the
197           current value of the cookie as an array.  In a scalar context it
198           will return the first value of a multivalued cookie.
199
200       domain()
201           Get or set the cookie's domain.
202
203       path()
204           Get or set the cookie's path.
205
206       expires()
207           Get or set the cookie's expiration time.
208

AUTHOR INFORMATION

210       Original version copyright 1997-1998, Lincoln D. Stein.  All rights
211       reserved.
212
213       This verson copyright 2001, Dr James Freeman.
214
215       This library is free software; you can redistribute it and/or modify it
216       under the same terms as Perl itself.
217
218       Address bug reports and comments to: jfreeman@tassie.net.au
219

BUGS

221       This section intentionally left blank :-)
222

SEE ALSO

224       CGI::Carp, CGI::Simple
225
226
227
228perl v5.8.8                       2004-11-22                 Simple::Cookie(3)
Impressum