📄 ch08_36.htm
字号:
<html><head><title>CGI::Cookie (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_35.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch08_37.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">8.36. CGI::Cookie</h2><p><a name="INDEX-1313" /><a name="INDEX-1314" />Providesan interface to Netscape (HTTP/1.1) cookies that can be used inconjunction with CGI.pm or independently. To use CGI::Cookie, createa new cookie object with the constructor <tt class="literal">new</tt>. Youcan then send the cookie to the browser in one of the following ways:</p><ul><li><p>From a CGI script, create a Set-Cookie field in the HTTP header foreach cookie you want to send (<tt class="literal">$c</tt> is the cookieobject):</p><blockquote><pre class="code">print "Set-Cookie: $c0";</pre></blockquote></li><li><p>With CGI.pm (see <a href="ch10_01.htm">Chapter 10, "The CGI.pm Module"</a>), use the<tt class="literal">header</tt> method with a <tt class="literal">-cookie</tt>argument:</p><blockquote><pre class="code">print header(-cookie=>$c);</pre></blockquote></li><li><p>Using <em class="emphasis">mod_perl</em> (see <a href="ch11_01.htm">Chapter 11, "Web Server Programmingwith mod_perl"</a>), use the request object's<tt class="literal">header_out</tt> method:</p><blockquote><pre class="code">$r->header_out('Set-Cookie',$c);</pre></blockquote><p>The following methods are provided for CGI::Cookie.</p></li></ul><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>new</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">c</em> <b class="emphasis-bold">=</b> new CGI::Cookie(<em class="replaceable">attribs</em>)</pre><p>Constructor. Creates a new cookie. Attributes are:</p><dl><dt><b><tt class="literal">-domain =</tt> <em class="replaceable">domain_name</em></b></dt><dd>Optional. Points to domain name or fully qualified hostname to whichcookie will be returned. If missing, browser will return cookie onlyto the server that set it.</p></dd><dt><b><tt class="literal">-expires =</tt> <em class="replaceable">date</em></b></dt><dd>Optional expiration date in any of the date formats recognized byCGI.pm. If missing, cookie expires at the end of this browsersession.</p></dd><dt><b><tt class="literal">-name =</tt> <em class="replaceable">name</em></b></dt><dd>Required. Scalar value with the cookie name.</p></dd><dt><b><tt class="literal">-path =</tt> <em class="replaceable">path</em></b></dt><dd>Optional. Points to a partial URL on the current server; cookies willbe returned to any URL beginning with this path. Defaults to<tt class="literal">/</tt>.</p></dd><dt><b><tt class="literal">-secure =</tt> <em class="replaceable">boolean</em></b></dt><dd>Optional. If true, browser will return cookie only if a cryptographicprotocol is in use.</p></dd><dt><b><tt class="literal">-value =</tt> <em class="replaceable">value</em></b></dt><dd>Required. The value of the cookie; can be a scalar, an arrayreference, or a hash reference.</p></dd></dl></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>as_string</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">c</em>->as_string</pre><p>Turns internal representation of cookie into RFC-compliant text.Called internally by overloading the"" operator, or can be calleddirectly.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>domain</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">c</em>->domain(<em class="replaceable">val</em>)</pre><p>Gets or sets the cookie's domain. With no parameter,gets the current value; otherwise, sets the new value.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>expires</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">c</em>->expires(<em class="replaceable">val</em>)</pre><p>Gets or sets the cookie's expiration date. With noparameter, gets the current expiration date; otherwise, sets the newvalue.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>fetch</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>%<em class="replaceable">cookies</em> = fetch CGI::Cookie</pre><p>Returns a hash containing cookies returned by the browser, in whichthe keys are the cookie names and the values are the cookie values.In a scalar context, <tt class="literal">fetch</tt> returns a hashreference.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>name</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">c</em>->name(<em class="replaceable">val</em>)</pre><p>Gets or sets the cookie's name. With no parameter,returns the current name; otherwise, sets the new value.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>parse</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>%<em class="replaceable">cookies</em> = parse CGI::Cookie(<em class="replaceable">stored_cookies</em>)</pre><p>Retrieves cookies stored in an external form.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>path</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">c</em>->path(<em class="replaceable">val</em>)</pre><p>Gets or sets the cookie's path. With no parameter,returns the current path; otherwise, sets the new value.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>raw_fetch</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>%<em class="replaceable">cookies</em> = raw_fetch CGI::Cookie</pre><p>Like <tt class="literal">fetch</tt>, but does no unescaping of reservedcharacters; useful for retrieving cookies set by a foreign server.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>value</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">c</em>->value(<em class="replaceable">val</em>)</pre><p>Gets or sets the cookie's value. With no parameter,returns the current value; otherwise, sets the new value. In arraycontext, returns the current value as an array. In scalar context,returns the first value of a multivalued cookie.</p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_35.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch08_37.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">8.35. CGI::Carp</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">8.37. CGI::Fast</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -