function.setcookie.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 407 行 · 第 1/2 页

HTML
407
字号
     <dd>      <p class="para">       When <b><tt>TRUE</tt></b> the cookie will be made accessible only through the HTTP       protocol. This means that the cookie won&#039;t be accessible by       scripting languages, such as JavaScript. This setting can effectly       help to reduce identity theft through XSS attacks (although it is       not supported by all browsers). Added in PHP 5.2.0.       <b><tt>TRUE</tt></b> or <b><tt>FALSE</tt></b>      </p>     </dd>    </dt>   </dl>  </p> </div> <div class="refsect1 returnvalues">  <h3 class="title">Return Values</h3>  <p class="para">   If output exists prior to calling this function,   <b>setcookie()</b> will fail and return <b><tt>FALSE</tt></b>. If   <b>setcookie()</b> successfully runs, it will return <b><tt>TRUE</tt></b>.   This does not indicate whether the user accepted the cookie.  </p> </div> <div class="refsect1 examples">  <h3 class="title">Examples</h3>  <p class="para">   Some examples follow how to send cookies:   <div class="example">    <p><b>Example #1 <b>setcookie()</b> send example</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$value&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'something&nbsp;from&nbsp;somewhere'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">setcookie</span><span style="color: #007700">(</span><span style="color: #DD0000">"TestCookie"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">setcookie</span><span style="color: #007700">(</span><span style="color: #DD0000">"TestCookie"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">time</span><span style="color: #007700">()+</span><span style="color: #0000BB">3600</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;expire&nbsp;in&nbsp;1&nbsp;hour&nbsp;*/<br /></span><span style="color: #0000BB">setcookie</span><span style="color: #007700">(</span><span style="color: #DD0000">"TestCookie"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">time</span><span style="color: #007700">()+</span><span style="color: #0000BB">3600</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"/~rasmus/"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">".example.com"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p>  <p class="para">   Note that the value portion of the cookie will automatically be   urlencoded when you send the cookie, and when it is received, it   is automatically decoded and assigned to a variable by the same   name as the cookie name. If you don&#039;t want this, you can use   <a href="function.setrawcookie.html" class="function">setrawcookie()</a> instead if you are using PHP 5. To see   the contents of our test cookie in a script, simply use one of the   following examples:  </p>  <p class="para">   <div class="informalexample">    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Print&nbsp;an&nbsp;individual&nbsp;cookie<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$_COOKIE</span><span style="color: #007700">[</span><span style="color: #DD0000">"TestCookie"</span><span style="color: #007700">];<br />echo&nbsp;</span><span style="color: #0000BB">$HTTP_COOKIE_VARS</span><span style="color: #007700">[</span><span style="color: #DD0000">"TestCookie"</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">//&nbsp;Another&nbsp;way&nbsp;to&nbsp;debug/test&nbsp;is&nbsp;to&nbsp;view&nbsp;all&nbsp;cookies<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$_COOKIE</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p>  <p class="para">   <div class="example">    <p><b>Example #2 <b>setcookie()</b> delete example</b></p>    <div class="example-contents"><p>     When deleting a cookie you should assure that the expiration date     is in the past, to trigger the removal mechanism in your browser.     Examples follow how to delete cookies sent in previous example:    </p></div>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;set&nbsp;the&nbsp;expiration&nbsp;date&nbsp;to&nbsp;one&nbsp;hour&nbsp;ago<br /></span><span style="color: #0000BB">setcookie&nbsp;</span><span style="color: #007700">(</span><span style="color: #DD0000">"TestCookie"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">time</span><span style="color: #007700">()&nbsp;-&nbsp;</span><span style="color: #0000BB">3600</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">setcookie&nbsp;</span><span style="color: #007700">(</span><span style="color: #DD0000">"TestCookie"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">time</span><span style="color: #007700">()&nbsp;-&nbsp;</span><span style="color: #0000BB">3600</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"/~rasmus/"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">".example.com"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p>  <p class="para">   <div class="example">    <p><b>Example #3 <b>setcookie()</b> and arrays</b></p>    <div class="example-contents"><p>     You may also set array cookies by using array notation in the     cookie name. This has the effect of setting as many cookies as     you have array elements, but when the cookie is received by your     script, the values are all placed in an array with the cookie&#039;s     name:    </p></div>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;set&nbsp;the&nbsp;cookies<br /></span><span style="color: #0000BB">setcookie</span><span style="color: #007700">(</span><span style="color: #DD0000">"cookie[three]"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"cookiethree"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">setcookie</span><span style="color: #007700">(</span><span style="color: #DD0000">"cookie[two]"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"cookietwo"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">setcookie</span><span style="color: #007700">(</span><span style="color: #DD0000">"cookie[one]"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"cookieone"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;after&nbsp;the&nbsp;page&nbsp;reloads,&nbsp;print&nbsp;them&nbsp;out<br /></span><span style="color: #007700">if&nbsp;(isset(</span><span style="color: #0000BB">$_COOKIE</span><span style="color: #007700">[</span><span style="color: #DD0000">'cookie'</span><span style="color: #007700">]))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$_COOKIE</span><span style="color: #007700">[</span><span style="color: #DD0000">'cookie'</span><span style="color: #007700">]&nbsp;as&nbsp;</span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"$name&nbsp;:&nbsp;$value&nbsp;&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>    <div class="example-contents"><p>The above example will output:</p></div>    <div class="example-contents"><pre><div class="cdata"><pre>three : cookiethreetwo : cookietwoone : cookieone</pre></div>    </pre></div>   </div>  </p> </div> <div class="refsect1 notes">  <h3 class="title">Notes</h3>  <blockquote><p><b class="note">Note</b>:        As of PHP 4, you can use output buffering to send output prior to the    call of this function, with the overhead of all of your output to the    browser being buffered in the server until you send it. You can do this    by calling <a href="function.ob-start.html" class="function">ob_start()</a> and    <a href="function.ob-end-flush.html" class="function">ob_end_flush()</a> in your script, or setting the    <i>output_buffering</i> configuration directive on in your    <var class="filename">php.ini</var> or server configuration files.   <br />  </p></blockquote>  <blockquote><p><b class="note">Note</b>:        If the PHP directive <a href="ini.core.html#ini.register-globals" class="link">register_globals</a>    is set to <i>on</i> then cookie values will also be made into    variables.  In our examples below, <var class="varname">$TestCookie</var> will    exist.  It&#039;s recommended to use <var class="varname"><a href="reserved.variables.cookies.html" class="classname">$_COOKIE</a></var>.   <br />  </p></blockquote>  <p class="para">   Common Pitfalls:   <ul class="itemizedlist">    <li class="listitem">     <span class="simpara">      Cookies will not become visible until the next loading of a page that      the cookie should be visible for.  To test if a cookie was successfully      set, check for the cookie on a next loading page before the cookie      expires.  Expire time is set via the <i><tt class="parameter">expire</tt></i>      parameter.  A nice way to debug the existence of cookies is by      simply calling <i>print_r($_COOKIE);</i>.     </span>    </li>    <li class="listitem">     <span class="simpara">      Cookies must be deleted with the same parameters as they were set with.      If the value argument is an empty string, or <b><tt>FALSE</tt></b>, and all other arguments      match a previous call to setcookie, then the cookie with the specified      name will be deleted from the remote client.     </span>    </li>    <li class="listitem">     <span class="simpara">      Because setting a cookie with a value of <b><tt>FALSE</tt></b> will try to delete the cookie,      you should not use boolean values. Instead, use <em class="emphasis">0</em> for <b><tt>FALSE</tt></b>      and <em class="emphasis">1</em> for <b><tt>TRUE</tt></b>.     </span>    </li>    <li class="listitem">     <span class="simpara">      Cookies names can be set as array names and will be available to your      PHP scripts as arrays but separate cookies are stored on the users      system. Consider <a href="function.explode.html" class="function">explode()</a> to set one cookie with      multiple names and values. It is not recommended to use      <a href="function.serialize.html" class="function">serialize()</a> for this purpose, because it can result      in security holes.     </span>    </li>   </ul>  </p>  <p class="simpara">   Multiple calls to <b>setcookie()</b> are performed in the order called.  </p> </div> <div class="refsect1 seealso">  <h3 class="title">See Also</h3>  <p class="para">   <ul class="simplelist">    <li class="member"><a href="function.header.html" class="function" rel="rdfs-seeAlso">header()</a></li>    <li class="member"><a href="function.setrawcookie.html" class="function" rel="rdfs-seeAlso">setrawcookie()</a></li>    <li class="member"><a href="features.cookies.html" class="link">cookies section</a></li>    <li class="member"><a href="http://www.faqs.org/rfcs/rfc2109" class="link external">&raquo; RFC 2109</a></li>    <li class="member"><a href="http://www.faqs.org/rfcs/rfc2965" class="link external">&raquo; RFC 2965</a></li>   </ul>  </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.pfsockopen.html">pfsockopen</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.setrawcookie.html">setrawcookie</a></div> <div class="up"><a href="ref.network.html">Network Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>

⌨️ 快捷键说明

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