.\" Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "CGI::Cookie 3pm" .TH CGI::Cookie 3pm "2018-08-21" "perl v5.26.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" CGI::Cookie \- Interface to HTTP Cookies .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use CGI qw/:standard/; \& use CGI::Cookie; \& \& # Create new cookies and send them \& $cookie1 = CGI::Cookie\->new(\-name=>\*(AqID\*(Aq,\-value=>123456); \& $cookie2 = CGI::Cookie\->new(\-name=>\*(Aqpreferences\*(Aq, \& \-value=>{ font => Helvetica, \& size => 12 } \& ); \& print header(\-cookie=>[$cookie1,$cookie2]); \& \& # fetch existing cookies \& %cookies = CGI::Cookie\->fetch; \& $id = $cookies{\*(AqID\*(Aq}\->value; \& \& # create cookies returned from an external source \& %cookies = CGI::Cookie\->parse($ENV{COOKIE}); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" CGI::Cookie is an interface to \s-1HTTP/1.1\s0 cookies, a mechanism that allows Web servers to store persistent information on the browser's side of the connection. Although CGI::Cookie is intended to be used in conjunction with \s-1CGI\s0.pm (and is in fact used by it internally), you can use this module independently. .PP For full information on cookies see .PP .Vb 1 \& https://tools.ietf.org/html/rfc6265 .Ve .SH "USING CGI::Cookie" .IX Header "USING CGI::Cookie" CGI::Cookie is object oriented. Each cookie object has a name and a value. The name is any scalar value. The value is any scalar or array value (associative arrays are also allowed). Cookies also have several optional attributes, including: .IP "\fB1. expiration date\fR" 4 .IX Item "1. expiration date" The expiration date tells the browser how long to hang on to the cookie. If the cookie specifies an expiration date in the future, the browser will store the cookie information in a disk file and return it to the server every time the user reconnects (until the expiration date is reached). If the cookie species an expiration date in the past, the browser will remove the cookie from the disk file. If the expiration date is not specified, the cookie will persist only until the user quits the browser. .IP "\fB2. domain\fR" 4 .IX Item "2. domain" This is a partial or complete domain name for which the cookie is valid. The browser will return the cookie to any host that matches the partial domain name. For example, if you specify a domain name of \*(L".capricorn.com\*(R", then the browser will return the cookie to Web servers running on any of the machines \*(L"www.capricorn.com\*(R", \&\*(L"ftp.capricorn.com\*(R", \*(L"feckless.capricorn.com\*(R", etc. Domain names must contain at least two periods to prevent attempts to match on top level domains like \*(L".edu\*(R". If no domain is specified, then the browser will only return the cookie to servers on the host the cookie originated from. .IP "\fB3. path\fR" 4 .IX Item "3. path" If you provide a cookie path attribute, the browser will check it against your script's \s-1URL\s0 before returning the cookie. For example, if you specify the path \*(L"/cgi\-bin\*(R", then the cookie will be returned to each of the scripts \*(L"/cgi\-bin/tally.pl\*(R", \*(L"/cgi\-bin/order.pl\*(R", and \&\*(L"/cgi\-bin/customer_service/complain.pl\*(R", but not to the script \&\*(L"/cgi\-private/site_admin.pl\*(R". By default, the path is set to \*(L"/\*(R", so that all scripts at your site will receive the cookie. .IP "\fB4. secure flag\fR" 4 .IX Item "4. secure flag" If the \*(L"secure\*(R" attribute is set, the cookie will only be sent to your script if the \s-1CGI\s0 request is occurring on a secure channel, such as \s-1SSL.\s0 .IP "\fB5. httponly flag\fR" 4 .IX Item "5. httponly flag" If the \*(L"httponly\*(R" attribute is set, the cookie will only be accessible through \s-1HTTP\s0 Requests. This cookie will be inaccessible via JavaScript (to prevent \s-1XSS\s0 attacks). .Sp This feature is supported by nearly all modern browsers. .Sp See these URLs for more information: .Sp .Vb 2 \& http://msdn.microsoft.com/en\-us/library/ms533046.aspx \& http://www.browserscope.org/?category=security&v=top .Ve .IP "\fB6. samesite flag\fR" 4 .IX Item "6. samesite flag" Allowed settings are \f(CW\*(C`Strict\*(C'\fR and \f(CW\*(C`Lax\*(C'\fR. .Sp As of June 2016, support is limited to recent releases of Chrome and Opera. .Sp .SS "Creating New Cookies" .IX Subsection "Creating New Cookies" .Vb 9 \& my $c = CGI::Cookie\->new(\-name => \*(Aqfoo\*(Aq, \& \-value => \*(Aqbar\*(Aq, \& \-expires => \*(Aq+3M\*(Aq, \& \*(Aq\-max\-age\*(Aq => \*(Aq+3M\*(Aq, \& \-domain => \*(Aq.capricorn.com\*(Aq, \& \-path => \*(Aq/cgi\-bin/database\*(Aq, \& \-secure => 1, \& \-samesite=> "Lax" \& ); .Ve .PP Create cookies from scratch with the \fBnew\fR method. The \fB\-name\fR and \&\fB\-value\fR parameters are required. The name must be a scalar value. The value can be a scalar, an array reference, or a hash reference. (At some point in the future cookies will support one of the Perl object serialization protocols for full generality). .PP \&\fB\-expires\fR accepts any of the relative or absolute date formats recognized by \s-1CGI\s0.pm, for example \*(L"+3M\*(R" for three months in the future. See \s-1CGI\s0.pm's documentation for details. .PP \&\fB\-max\-age\fR accepts the same data formats as \fB\-expires\fR, but sets a relative value instead of an absolute like \fB\-expires\fR. This is intended to be more secure since a clock could be changed to fake an absolute time. In practice, as of 2011, \f(CW\*(C`\-max\-age\*(C'\fR still does not enjoy the widespread support that \f(CW\*(C`\-expires\*(C'\fR has. You can set both, and browsers that support \&\f(CW\*(C`\-max\-age\*(C'\fR should ignore the \f(CW\*(C`Expires\*(C'\fR header. The drawback to this approach is the bit of bandwidth for sending an extra header on each cookie. .PP \&\fB\-domain\fR points to a domain name or to a fully qualified host name. If not specified, the cookie will be returned only to the Web server that created it. .PP \&\fB\-path\fR points to a partial \s-1URL\s0 on the current server. The cookie will be returned to all URLs beginning with the specified path. If not specified, it defaults to '/', which returns the cookie to all pages at your site. .PP \&\fB\-secure\fR if set to a true value instructs the browser to return the cookie only when a cryptographic protocol is in use. .PP \&\fB\-httponly\fR if set to a true value, the cookie will not be accessible via JavaScript. .PP \&\fB\-samesite\fR may be \f(CW\*(C`Lax\*(C'\fR or \f(CW\*(C`Strict\*(C'\fR and is an evolving part of the standards for cookies. Please refer to current documentation regarding it. .PP For compatibility with Apache::Cookie, you may optionally pass in a mod_perl request object as the first argument to \f(CW\*(C`new()\*(C'\fR. It will simply be ignored: .PP .Vb 3 \& my $c = CGI::Cookie\->new($r, \& \-name => \*(Aqfoo\*(Aq, \& \-value => [\*(Aqbar\*(Aq,\*(Aqbaz\*(Aq]); .Ve .SS "Sending the Cookie to the Browser" .IX Subsection "Sending the Cookie to the Browser" The simplest way to send a cookie to the browser is by calling the \fIbake()\fR method: .PP .Vb 1 \& $c\->bake; .Ve .PP This will print the Set-Cookie \s-1HTTP\s0 header to \s-1STDOUT\s0 using \s-1CGI\s0.pm. \s-1CGI\s0.pm will be loaded for this purpose if it is not already. Otherwise \s-1CGI\s0.pm is not required or used by this module. .PP Under mod_perl, pass in an Apache request object: .PP .Vb 1 \& $c\->bake($r); .Ve .PP If you want to set the cookie yourself, Within a \s-1CGI\s0 script you can send a cookie to the browser by creating one or more Set-Cookie: fields in the \&\s-1HTTP\s0 header. Here is a typical sequence: .PP .Vb 3 \& my $c = CGI::Cookie\->new(\-name => \*(Aqfoo\*(Aq, \& \-value => [\*(Aqbar\*(Aq,\*(Aqbaz\*(Aq], \& \-expires => \*(Aq+3M\*(Aq); \& \& print "Set\-Cookie: $c\en"; \& print "Content\-Type: text/html\en\en"; .Ve .PP To send more than one cookie, create several Set-Cookie: fields. .PP If you are using \s-1CGI\s0.pm, you send cookies by providing a \-cookie argument to the \fIheader()\fR method: .PP .Vb 1 \& print header(\-cookie=>$c); .Ve .PP Mod_perl users can set cookies using the request object's \fIheader_out()\fR method: .PP .Vb 1 \& $r\->headers_out\->set(\*(AqSet\-Cookie\*(Aq => $c); .Ve .PP Internally, Cookie overloads the "" operator to call its \fIas_string()\fR method when incorporated into the \s-1HTTP\s0 header. \fIas_string()\fR turns the Cookie's internal representation into an RFC-compliant text representation. You may call \fIas_string()\fR yourself if you prefer: .PP .Vb 1 \& print "Set\-Cookie: ",$c\->as_string,"\en"; .Ve .SS "Recovering Previous Cookies" .IX Subsection "Recovering Previous Cookies" .Vb 1 \& %cookies = CGI::Cookie\->fetch; .Ve .PP \&\fBfetch\fR returns an associative array consisting of all cookies returned by the browser. The keys of the array are the cookie names. You can iterate through the cookies this way: .PP .Vb 4 \& %cookies = CGI::Cookie\->fetch; \& for (keys %cookies) { \& do_something($cookies{$_}); \& } .Ve .PP In a scalar context, \fIfetch()\fR returns a hash reference, which may be more efficient if you are manipulating multiple cookies. .PP \&\s-1CGI\s0.pm uses the \s-1URL\s0 escaping methods to save and restore reserved characters in its cookies. If you are trying to retrieve a cookie set by a foreign server, this escaping method may trip you up. Use \fIraw_fetch()\fR instead, which has the same semantics as \fIfetch()\fR, but performs no unescaping. .PP You may also retrieve cookies that were stored in some external form using the \fIparse()\fR class method: .PP .Vb 2 \& $COOKIES = \`cat /usr/tmp/Cookie_stash\`; \& %cookies = CGI::Cookie\->parse($COOKIES); .Ve .PP If you are in a mod_perl environment, you can save some overhead by passing the request object to \fIfetch()\fR like this: .PP .Vb 1 \& CGI::Cookie\->fetch($r); .Ve .PP If the value passed to \fIparse()\fR is undefined, an empty array will returned in list context, and an empty hashref will be returned in scalar context. .SS "Manipulating Cookies" .IX Subsection "Manipulating Cookies" Cookie objects have a series of accessor methods to get and set cookie attributes. Each accessor has a similar syntax. Called without arguments, the accessor returns the current value of the attribute. Called with an argument, the accessor changes the attribute and returns its new value. .IP "\fB\f(BIname()\fB\fR" 4 .IX Item "name()" Get or set the cookie's name. Example: .Sp .Vb 2 \& $name = $c\->name; \& $new_name = $c\->name(\*(Aqfred\*(Aq); .Ve .IP "\fB\f(BIvalue()\fB\fR" 4 .IX Item "value()" Get or set the cookie's value. Example: .Sp .Vb 2 \& $value = $c\->value; \& @new_value = $c\->value([\*(Aqa\*(Aq,\*(Aqb\*(Aq,\*(Aqc\*(Aq,\*(Aqd\*(Aq]); .Ve .Sp \&\fB\f(BIvalue()\fB\fR is context sensitive. In a list context it will return the current value of the cookie as an array. In a scalar context it will return the \fBfirst\fR value of a multivalued cookie. .IP "\fB\f(BIdomain()\fB\fR" 4 .IX Item "domain()" Get or set the cookie's domain. .IP "\fB\f(BIpath()\fB\fR" 4 .IX Item "path()" Get or set the cookie's path. .IP "\fB\f(BIexpires()\fB\fR" 4 .IX Item "expires()" Get or set the cookie's expiration time. .IP "\fB\f(BImax_age()\fB\fR" 4 .IX Item "max_age()" Get or set the cookie's max_age value. .SH "AUTHOR INFORMATION" .IX Header "AUTHOR INFORMATION" The \s-1CGI\s0.pm distribution is copyright 1995\-2007, Lincoln D. Stein. It is distributed under \s-1GPL\s0 and the Artistic License 2.0. It is currently maintained by Lee Johnson with help from many contributors. .PP Address bug reports and comments to: https://github.com/leejo/CGI.pm/issues .PP The original bug tracker can be found at: https://rt.cpan.org/Public/Dist/Display.html?Queue=CGI.pm .PP When sending bug reports, please provide the version of \s-1CGI\s0.pm, the version of Perl, the name and version of your Web server, and the name and version of the operating system you are using. If the problem is even remotely browser dependent, please provide information about the affected browsers as well. .SH "BUGS" .IX Header "BUGS" This section intentionally left blank. .SH "SEE ALSO" .IX Header "SEE ALSO" CGI::Carp, \s-1CGI\s0 .PP \&\s-1RFC 2109\s0 , \s-1RFC 2695\s0