cookie.html

来自「perl教程」· HTML 代码 · 共 326 行 · 第 1/2 页

HTML
326 行
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>CGI::Cookie - Interface to Netscape Cookies</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>CGI::Cookie - Interface to Netscape Cookies</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#synopsis">SYNOPSIS</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<li><a href="#using_cgi__cookie">USING CGI::Cookie</a></li>
	<ul>

		<li><a href="#creating_new_cookies">Creating New Cookies</a></li>
		<li><a href="#sending_the_cookie_to_the_browser">Sending the Cookie to the Browser</a></li>
		<li><a href="#recovering_previous_cookies">Recovering Previous Cookies</a></li>
		<li><a href="#manipulating_cookies">Manipulating Cookies</a></li>
	</ul>

	<li><a href="#author_information">AUTHOR INFORMATION</a></li>
	<li><a href="#bugs">BUGS</a></li>
	<li><a href="#see_also">SEE ALSO</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>CGI::Cookie - Interface to Netscape Cookies</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
    <span class="keyword">use</span> <span class="variable">CGI</span> <span class="string">qw/:standard/</span><span class="operator">;</span>
    <span class="keyword">use</span> <span class="variable">CGI::Cookie</span><span class="operator">;</span>
</pre>
<pre>
    <span class="comment"># Create new cookies and send them</span>
    <span class="variable">$cookie1</span> <span class="operator">=</span> <span class="variable">new</span> <span class="variable">CGI::Cookie</span><span class="operator">(</span><span class="string">-name</span><span class="operator">=&gt;</span><span class="string">'ID'</span><span class="operator">,</span><span class="string">-value</span><span class="operator">=&gt;</span><span class="number">123456</span><span class="operator">);</span>
    <span class="variable">$cookie2</span> <span class="operator">=</span> <span class="variable">new</span> <span class="variable">CGI::Cookie</span><span class="operator">(</span><span class="string">-name</span><span class="operator">=&gt;</span><span class="string">'preferences'</span><span class="operator">,</span>
                               <span class="string">-value</span><span class="operator">=&gt;</span><span class="operator">{</span> <span class="string">font</span> <span class="operator">=&gt;</span> <span class="variable">Helvetica</span><span class="operator">,</span>
                                         <span class="string">size</span> <span class="operator">=&gt;</span> <span class="number">12</span> <span class="operator">}</span> 
                               <span class="operator">);</span>
    <span class="keyword">print</span> <span class="variable">header</span><span class="operator">(</span><span class="string">-cookie</span><span class="operator">=&gt;</span><span class="operator">[</span><span class="variable">$cookie1</span><span class="operator">,</span><span class="variable">$cookie2</span><span class="operator">]</span><span class="operator">);</span>
</pre>
<pre>
    <span class="comment"># fetch existing cookies</span>
    <span class="variable">%cookies</span> <span class="operator">=</span> <span class="variable">fetch</span> <span class="variable">CGI::Cookie</span><span class="operator">;</span>
    <span class="variable">$id</span> <span class="operator">=</span> <span class="variable">$cookies</span><span class="operator">{</span><span class="string">'ID'</span><span class="operator">}</span><span class="operator">-&gt;</span><span class="variable">value</span><span class="operator">;</span>
</pre>
<pre>
    <span class="comment"># create cookies returned from an external source</span>
    <span class="variable">%cookies</span> <span class="operator">=</span> <span class="variable">parse</span> <span class="variable">CGI::Cookie</span><span class="operator">(</span><span class="variable">$ENV</span><span class="operator">{</span><span class="string">COOKIE</span><span class="operator">}</span><span class="operator">);</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>CGI::Cookie is an interface to Netscape (HTTP/1.1) 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 CGI.pm (and is in fact used by
it internally), you can use this module independently.</p>
<p>For full information on cookies see</p>
<pre>
        <a href="http://www.ics.uci.edu/pub/ietf/http/rfc2109.txt">http://www.ics.uci.edu/pub/ietf/http/rfc2109.txt</a></pre>
<p>
</p>
<hr />
<h1><a name="using_cgi__cookie">USING CGI::Cookie</a></h1>
<p>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:</p>
<ol>
<li><strong><a name="item_expiration_date"><strong>1. expiration date</strong></a></strong>

<p>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.</p>
</li>
<li><strong><a name="item_domain"><strong>2. domain</strong></a></strong>

<p>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 &quot;.capricorn.com&quot;, then Netscape will return the cookie to
Web servers running on any of the machines &quot;www.capricorn.com&quot;, 
&quot;ftp.capricorn.com&quot;, &quot;feckless.capricorn.com&quot;, etc.  Domain names
must contain at least two periods to prevent attempts to match
on top level domains like &quot;.edu&quot;.  If no domain is specified, then
the browser will only return the cookie to servers on the host the
cookie originated from.</p>
</li>
<li><strong><a name="item_path"><strong>3. path</strong></a></strong>

<p>If you provide a cookie path attribute, the browser will check it
against your script's URL before returning the cookie.  For example,
if you specify the path &quot;/cgi-bin&quot;, then the cookie will be returned
to each of the scripts &quot;/cgi-bin/tally.pl&quot;, &quot;/cgi-bin/order.pl&quot;, and
&quot;/cgi-bin/customer_service/complain.pl&quot;, but not to the script
&quot;/cgi-private/site_admin.pl&quot;.  By default, the path is set to &quot;/&quot;, so
that all scripts at your site will receive the cookie.</p>
</li>
<li><strong><a name="item_secure_flag"><strong>4. secure flag</strong></a></strong>

<p>If the &quot;secure&quot; attribute is set, the cookie will only be sent to your
script if the CGI request is occurring on a secure channel, such as SSL.</p>
</li>
</ol>
<p>
</p>
<h2><a name="creating_new_cookies">Creating New Cookies</a></h2>
<pre>
        <span class="keyword">my</span> <span class="variable">$c</span> <span class="operator">=</span> <span class="variable">new</span> <span class="variable">CGI::Cookie</span><span class="operator">(</span><span class="string">-name</span>    <span class="operator">=&gt;</span>  <span class="string">'foo'</span><span class="operator">,</span>
                             <span class="string">-value</span>   <span class="operator">=&gt;</span>  <span class="string">'bar'</span><span class="operator">,</span>
                             <span class="string">-expires</span> <span class="operator">=&gt;</span>  <span class="string">'+3M'</span><span class="operator">,</span>
                             <span class="string">-domain</span>  <span class="operator">=&gt;</span>  <span class="string">'.capricorn.com'</span><span class="operator">,</span>
                             <span class="string">-path</span>    <span class="operator">=&gt;</span>  <span class="string">'/cgi-bin/database'</span><span class="operator">,</span>
                             <span class="string">-secure</span>  <span class="operator">=&gt;</span>  <span class="number">1</span>
                            <span class="operator">);</span>
</pre>
<p>Create cookies from scratch with the <strong>new</strong> method.  The <strong>-name</strong> and
<strong>-value</strong> 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).</p>
<p><strong>-expires</strong> accepts any of the relative or absolute date formats
recognized by CGI.pm, for example &quot;+3M&quot; for three months in the
future.  See CGI.pm's documentation for details.</p>
<p><strong>-domain</strong> 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.</p>
<p><strong>-path</strong> points to a partial URL 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.</p>
<p><strong>-secure</strong> if set to a true value instructs the browser to return the

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?