📄 cgi::cookie.3
字号:
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05).\".\" Standard preamble:.\" ========================================================================.de Sh \" Subsection heading.br.if t .Sp.ne 5.PP\fB\\$1\fR.PP...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" '''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 (.Sh), items (.Ip), and index.\" entries marked with X<> in POD. Of course, you'll have to process the.\" output yourself in some meaningful fashion..ie \nF \{\. de IX. tm Index:\\$1\t\\n%\t"\\$2"... nr % 0. rr F.\}.el \{\. de IX...\}.\".\" 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 "2007-12-18" "perl v5.10.0" "Perl Programmers Reference Guide".\" 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 Netscape Cookies.SH "SYNOPSIS".IX Header "SYNOPSIS".Vb 2\& use CGI qw/:standard/;\& use CGI::Cookie;\&\& # Create new cookies and send them\& $cookie1 = new CGI::Cookie(\-name=>\*(AqID\*(Aq,\-value=>123456);\& $cookie2 = new CGI::Cookie(\-name=>\*(Aqpreferences\*(Aq,\& \-value=>{ font => Helvetica,\& size => 12 } \& );\& print header(\-cookie=>[$cookie1,$cookie2]);\&\& # fetch existing cookies\& %cookies = fetch CGI::Cookie;\& $id = $cookies{\*(AqID\*(Aq}\->value;\&\& # create cookies returned from an external source\& %cookies = parse CGI::Cookie($ENV{COOKIE});.Ve.SH "DESCRIPTION".IX Header "DESCRIPTION"CGI::Cookie is an interface to Netscape (\s-1HTTP/1\s0.1) cookies, aninnovation that allows Web servers to store persistent information onthe browser's side of the connection. Although CGI::Cookie isintended to be used in conjunction with \s-1CGI\s0.pm (and is in fact used byit internally), you can use this module independently..PPFor full information on cookies see.PP.Vb 1\& http://www.ics.uci.edu/pub/ietf/http/rfc2109.txt.Ve.SH "USING CGI::Cookie".IX Header "USING CGI::Cookie"CGI::Cookie is object oriented. Each cookie object has a name and avalue. The name is any scalar value. The value is any scalar orarray value (associative arrays are also allowed). Cookies also haveseveral 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 thecookie. If the cookie specifies an expiration date in the future, thebrowser will store the cookie information in a disk file and return itto the server every time the user reconnects (until the expirationdate is reached). If the cookie species an expiration date in thepast, the browser will remove the cookie from the disk file. If theexpiration date is not specified, the cookie will persist only untilthe 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 matchesthe partial domain name. For example, if you specify a domain nameof \*(L".capricorn.com\*(R", then Netscape will return the cookie toWeb servers running on any of the machines \*(L"www.capricorn.com\*(R", \&\*(L"ftp.capricorn.com\*(R", \*(L"feckless.capricorn.com\*(R", etc. Domain namesmust contain at least two periods to prevent attempts to matchon top level domains like \*(L".edu\*(R". If no domain is specified, thenthe browser will only return the cookie to servers on the host thecookie originated from..IP "\fB3. path\fR" 4.IX Item "3. path"If you provide a cookie path attribute, the browser will check itagainst 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 returnedto 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", sothat 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 yourscript if the \s-1CGI\s0 request is occurring on a secure channel, such as \s-1SSL\s0..IP "\fB4. httponly flag\fR" 4.IX Item "4. httponly flag"If the \*(L"httponly\*(R" attribute is set, the cookie will only be accessiblethrough \s-1HTTP\s0 Requests. This cookie will be inaccessible via JavaScript(to prevent \s-1XSS\s0 attacks)..SpBut, currently this feature only used and recognised by \&\s-1MS\s0 Internet Explorer 6 Service Pack 1 and later..SpSee this \s-1URL\s0 for more information:.Sp<http://msdn.microsoft.com/workshop/author/dhtml/httponly_cookies.asp>.Sh "Creating New Cookies".IX Subsection "Creating New Cookies".Vb 7\& my $c = new CGI::Cookie(\-name => \*(Aqfoo\*(Aq,\& \-value => \*(Aqbar\*(Aq,\& \-expires => \*(Aq+3M\*(Aq,\& \-domain => \*(Aq.capricorn.com\*(Aq,\& \-path => \*(Aq/cgi\-bin/database\*(Aq,\& \-secure => 1\& );.Ve.PPCreate 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 Perlobject serialization protocols for full generality)..PP\&\fB\-expires\fR accepts any of the relative or absolute date formatsrecognized by \s-1CGI\s0.pm, for example \*(L"+3M\*(R" for three months in thefuture. See \s-1CGI\s0.pm's documentation for details..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 serverthat created it..PP\&\fB\-path\fR points to a partial \s-1URL\s0 on the current server. The cookiewill be returned to all URLs beginning with the specified path. Ifnot specified, it defaults to '/', which returns the cookie to allpages at your site..PP\&\fB\-secure\fR if set to a true value instructs the browser to return thecookie only when a cryptographic protocol is in use..PP\&\fB\-httponly\fR if set to a true value, the cookie will not be accessiblevia JavaScript..PPFor compatibility with Apache::Cookie, you may optionally pass ina mod_perl request object as the first argument to \f(CW\*(C`new()\*(C'\fR. It willsimply be ignored:.PP.Vb 3\& my $c = new CGI::Cookie($r,\& \-name => \*(Aqfoo\*(Aq,\& \-value => [\*(Aqbar\*(Aq,\*(Aqbaz\*(Aq]);.Ve.Sh "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()\fRmethod:.PP.Vb 1\& $c\->bake;.Ve.PPUnder mod_perl, pass in an Apache request object:.PP.Vb 1\& $c\->bake($r);.Ve.PPIf you want to set the cookie yourself, Within a \s-1CGI\s0 script you can senda 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 = new CGI::Cookie(\-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.PPTo send more than one cookie, create several Set-Cookie: fields..PPIf you are using \s-1CGI\s0.pm, you send cookies by providing a \-cookieargument to the \fIheader()\fR method:.PP.Vb 1\& print header(\-cookie=>$c);.Ve.PPMod_perl users can set cookies using the request object's \fIheader_out()\fRmethod:.PP.Vb 1\& $r\->headers_out\->set(\*(AqSet\-Cookie\*(Aq => $c);.Ve.PPInternally, Cookie overloads the "" operator to call its \fIas_string()\fRmethod when incorporated into the \s-1HTTP\s0 header. \fIas_string()\fR turns theCookie's internal representation into an RFC-compliant textrepresentation. You may call \fIas_string()\fR yourself if you prefer:.PP.Vb 1\& print "Set\-Cookie: ",$c\->as_string,"\en";.Ve.Sh "Recovering Previous Cookies".IX Subsection "Recovering Previous Cookies".Vb 1\& %cookies = fetch CGI::Cookie;.Ve.PP\&\fBfetch\fR returns an associative array consisting of all cookiesreturned by the browser. The keys of the array are the cookie names. Youcan iterate through the cookies this way:.PP.Vb 4\& %cookies = fetch CGI::Cookie;\& foreach (keys %cookies) {\& do_something($cookies{$_});\& }.Ve.PPIn a scalar context, \fIfetch()\fR returns a hash reference, which may be moreefficient if you are manipulating multiple cookies..PP\&\s-1CGI\s0.pm uses the \s-1URL\s0 escaping methods to save and restore reserved charactersin 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 thesame semantics as \fIfetch()\fR, but performs no unescaping..PPYou may also retrieve cookies that were stored in some externalform using the \fIparse()\fR class method:.PP.Vb 2\& $COOKIES = \`cat /usr/tmp/Cookie_stash\`;\& %cookies = parse CGI::Cookie($COOKIES);.Ve.PPIf you are in a mod_perl environment, you can save some overhead bypassing the request object to \fIfetch()\fR like this:.PP.Vb 1\& CGI::Cookie\->fetch($r);.Ve.Sh "Manipulating Cookies".IX Subsection "Manipulating Cookies"Cookie objects have a series of accessor methods to get and set cookieattributes. Each accessor has a similar syntax. Called withoutarguments, the accessor returns the current value of the attribute.Called with an argument, the accessor changes the attribute andreturns 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 returnthe current value of the cookie as an array. In a scalar context itwill 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..PPThis library is free software; you can redistribute it and/or modifyit under the same terms as Perl itself..PPAddress 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -