.\" Automatically generated by Pod::Man 2.27 (Pod::Simple 3.28)
.\"
.\" 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 turned on, 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
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{
.    if \nF \{
.        de IX
.        tm Index:\\$1\t\\n%\t"\\$2"
..
.        if !\nF==2 \{
.            nr % 0
.            nr F 2
.        \}
.    \}
.\}
.rr rF
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear.  Run.  Save yourself.  No user-serviceable parts.
.    \" fudge factors for nroff and troff
.if n \{\
.    ds #H 0
.    ds #V .8m
.    ds #F .3m
.    ds #[ \f1
.    ds #] \fP
.\}
.if t \{\
.    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
.    ds #V .6m
.    ds #F 0
.    ds #[ \&
.    ds #] \&
.\}
.    \" simple accents for nroff and troff
.if n \{\
.    ds ' \&
.    ds ` \&
.    ds ^ \&
.    ds , \&
.    ds ~ ~
.    ds /
.\}
.if t \{\
.    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
.    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
.    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
.    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
.    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
.    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
.    \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
.    \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
.    \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
.    ds : e
.    ds 8 ss
.    ds o a
.    ds d- d\h'-1'\(ga
.    ds D- D\h'-1'\(hy
.    ds th \o'bp'
.    ds Th \o'LP'
.    ds ae ae
.    ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "CGI::Cookie 3"
.TH CGI::Cookie 3 "2012-08-16" "perl v5.16.3" "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, an
innovation 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 3
\&        http://tools.ietf.org/html/rfc2109
\&        http://tools.ietf.org/html/rfc2965
\&        http://tools.ietf.org/html/draft\-ietf\-httpstate\-cookie
.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 only supported by recent browsers like Internet Explorer
6 Service Pack 1, Firefox 3.0 and Opera 9.5 (and later of course).
.Sp
See these URLs for more information:
.Sp
.Vb 2
\&        http://msdn.microsoft.com/en\-us/library/ms533046.aspx
\&        http://www.owasp.org/index.php/HTTPOnly#Browsers_Supporting_HTTPOnly
.Ve
.SS "Creating New Cookies"
.IX Subsection "Creating New Cookies"
.Vb 7
\&        my $c = CGI::Cookie\->new(\-name    =>  \*(Aqfoo\*(Aq,
\&                             \-value   =>  \*(Aqbar\*(Aq,
\&                             \-expires =>  \*(Aq+3M\*(Aq,
\&                             \-domain  =>  \*(Aq.capricorn.com\*(Aq,
\&                             \-path    =>  \*(Aq/cgi\-bin/database\*(Aq,
\&                             \-secure  =>  1
\&                            );
.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
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.
.SH "AUTHOR INFORMATION"
.IX Header "AUTHOR INFORMATION"
Copyright 1997\-1998, Lincoln D. Stein.  All rights reserved.
.PP
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
.PP
Address bug reports and comments to: lstein@cshl.org
.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 <http://www.ietf.org/rfc/rfc2109.txt>, \s-1RFC 2695\s0 <http://www.ietf.org/rfc/rfc2965.txt>
