1CGI::Cookie(3pm) Perl Programmers Reference Guide CGI::Cookie(3pm)
2
3
4
6 CGI::Cookie - Interface to Netscape Cookies
7
9 use CGI qw/:standard/;
10 use CGI::Cookie;
11
12 # Create new cookies and send them
13 $cookie1 = new CGI::Cookie(-name=>'ID',-value=>123456);
14 $cookie2 = new CGI::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::Cookie;
22 $id = $cookies{'ID'}->value;
23
24 # create cookies returned from an external source
25 %cookies = parse CGI::Cookie($ENV{COOKIE});
26
28 CGI::Cookie is an interface to Netscape (HTTP/1.1) cookies, an innova‐
29 tion that allows Web servers to store persistent information on the
30 browser's side of the connection. Although CGI::Cookie is intended to
31 be used in conjunction with CGI.pm (and is in fact used by it inter‐
32 nally), 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
39 CGI::Cookie is object oriented. Each cookie object has a name and a
40 value. The name is any scalar value. The value is any scalar or array
41 value (associative arrays are also allowed). Cookies also have several
42 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::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.pm, for example "+3M" for three months in the future. See
99 CGI.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 my $c = new CGI::Cookie(-name => 'foo',
120 -value => ['bar','baz'],
121 -expires => '+3M');
122
123 print "Set-Cookie: $c\n";
124 print "Content-Type: text/html\n\n";
125
126 To send more than one cookie, create several Set-Cookie: fields.
127
128 If you are using CGI.pm, you send cookies by providing a -cookie argu‐
129 ment to the header() method:
130
131 print header(-cookie=>$c);
132
133 Mod_perl users can set cookies using the request object's header_out()
134 method:
135
136 $r->headers_out->set('Set-Cookie' => $c);
137
138 Internally, Cookie overloads the "" operator to call its as_string()
139 method when incorporated into the HTTP header. as_string() turns the
140 Cookie's internal representation into an RFC-compliant text representa‐
141 tion. You may call as_string() yourself if you prefer:
142
143 print "Set-Cookie: ",$c->as_string,"\n";
144
145 Recovering Previous Cookies
146
147 %cookies = fetch CGI::Cookie;
148
149 fetch returns an associative array consisting of all cookies returned
150 by the browser. The keys of the array are the cookie names. You can
151 iterate through the cookies this way:
152
153 %cookies = fetch CGI::Cookie;
154 foreach (keys %cookies) {
155 do_something($cookies{$_});
156 }
157
158 In a scalar context, fetch() returns a hash reference, which may be
159 more efficient if you are manipulating multiple cookies.
160
161 CGI.pm uses the URL escaping methods to save and restore reserved char‐
162 acters in its cookies. If you are trying to retrieve a cookie set by a
163 foreign server, this escaping method may trip you up. Use raw_fetch()
164 instead, which has the same semantics as fetch(), but performs no
165 unescaping.
166
167 You may also retrieve cookies that were stored in some external form
168 using the parse() class method:
169
170 $COOKIES = `cat /var/run/www/Cookie_stash`;
171 %cookies = parse CGI::Cookie($COOKIES);
172
173 If you are in a mod_perl environment, you can save some overhead by
174 passing the request object to fetch() like this:
175
176 CGI::Cookie->fetch($r);
177
178 Manipulating Cookies
179
180 Cookie objects have a series of accessor methods to get and set cookie
181 attributes. Each accessor has a similar syntax. Called without argu‐
182 ments, the accessor returns the current value of the attribute. Called
183 with an argument, the accessor changes the attribute and returns its
184 new value.
185
186 name()
187 Get or set the cookie's name. Example:
188
189 $name = $c->name;
190 $new_name = $c->name('fred');
191
192 value()
193 Get or set the cookie's value. Example:
194
195 $value = $c->value;
196 @new_value = $c->value(['a','b','c','d']);
197
198 value() is context sensitive. In a list context it will return the
199 current value of the cookie as an array. In a scalar context it
200 will return the first value of a multivalued cookie.
201
202 domain()
203 Get or set the cookie's domain.
204
205 path()
206 Get or set the cookie's path.
207
208 expires()
209 Get or set the cookie's expiration time.
210
212 Copyright 1997-1998, Lincoln D. Stein. All rights reserved.
213
214 This library is free software; you can redistribute it and/or modify it
215 under the same terms as Perl itself.
216
217 Address bug reports and comments to: lstein@cshl.org
218
220 This section intentionally left blank.
221
223 CGI::Carp, CGI
224
225
226
227perl v5.8.8 2001-09-21 CGI::Cookie(3pm)