function.setcookie.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 407 行 · 第 1/2 页
HTML
407 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Send a cookie</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><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><hr /><div id="function.setcookie" class="refentry"> <div class="refnamediv"> <h1 class="refname">setcookie</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">setcookie</span> — <span class="dc-title">Send a cookie</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><b><b>setcookie</b></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$name</tt></span> [, <span class="methodparam"><span class="type">string</span> <tt class="parameter">$value</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$expire</tt></span> [, <span class="methodparam"><span class="type">string</span> <tt class="parameter">$path</tt></span> [, <span class="methodparam"><span class="type">string</span> <tt class="parameter">$domain</tt></span> [, <span class="methodparam"><span class="type">bool</span> <tt class="parameter">$secure</tt></span> [, <span class="methodparam"><span class="type">bool</span> <tt class="parameter">$httponly</tt></span> ]]]]]] )</div> <p class="para rdfs-comment"> <b>setcookie()</b> defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent <em class="emphasis">before</em> any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <i><html></i> and <i><head></i> tags as well as any whitespace. </p> <p class="para"> Once the cookies have been set, they can be accessed on the next page load with the <var class="varname"><a href="reserved.variables.cookies.html" class="classname">$_COOKIE</a></var> or <var class="varname">$HTTP_COOKIE_VARS</var> arrays. Note, <a href="language.variables.superglobals.html" class="link">superglobals</a> such as <var class="varname"><a href="reserved.variables.cookies.html" class="classname">$_COOKIE</a></var> became available in PHP 4.1.0. Cookie values also exist in <var class="varname"><a href="reserved.variables.request.html" class="classname">$_REQUEST</a></var>. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> All the arguments except the <i><tt class="parameter">name</tt></i> argument are optional. You may also replace an argument with an empty string (<em class="emphasis">""</em>) in order to skip that argument. Because the <i><tt class="parameter">expire</tt></i> argument is integer, it cannot be skipped with an empty string, use a zero (<em class="emphasis">0</em>) instead. </p> <p class="para"> See <a href="http://cgi.netscape.com/newsref/std/cookie_spec.html" class="link external">» Netscape cookie specification</a> for specifics on how each <b>setcookie()</b> parameter works <dl> <dt> <span class="term"><i><tt class="parameter">name</tt></i></span> <dd> <p class="para"> The name of the cookie. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">value</tt></i></span> <dd> <p class="para"> The value of the cookie. This value is stored on the clients computer; do not store sensitive information. Assuming the <i><tt class="parameter">name</tt></i> is 'cookiename', this value is retrieved through <var class="varname"><a href="reserved.variables.cookies.html" class="classname">$_COOKIE['cookiename']</a></var> </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">expire</tt></i></span> <dd> <p class="para"> The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the <a href="function.time.html" class="function">time()</a> function plus the number of seconds before you want it to expire. Or you might use <a href="function.mktime.html" class="function">mktime()</a>. <i>time()+60*60*24*30</i> will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes). </p> <p class="para"> <blockquote><p><b class="note">Note</b>: You may notice the <i><tt class="parameter">expire</tt></i> parameter takes on a Unix timestamp, as opposed to the date format <i>Wdy, DD-Mon-YYYY HH:MM:SS GMT</i>, this is because PHP does this conversion internally. <br /> <i><tt class="parameter">expire</tt></i> is compared to the client's time which can differ from server's time. <br /> </p></blockquote> </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">path</tt></i></span> <dd> <p class="para"> The path on the server in which the cookie will be available on. If set to <i>'/'</i>, the cookie will be available within the entire <i><tt class="parameter">domain</tt></i>. If set to <i>'/foo/'</i>, the cookie will only be available within the <i>/foo/</i> directory and all sub-directories such as <i>/foo/bar/</i> of <i><tt class="parameter">domain</tt></i>. The default value is the current directory that the cookie is being set in. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">domain</tt></i></span> <dd> <p class="para"> The domain that the cookie is available. To make the cookie available on all subdomains of example.com then you'd set it to <i>'.example.com'</i>. The <i>.</i> is not required but makes it compatible with more browsers. Setting it to <i>www.example.com</i> will make the cookie only available in the <i>www</i> subdomain. Refer to tail matching in the <a href="http://cgi.netscape.com/newsref/std/cookie_spec.html" class="link external">» spec</a> for details. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">secure</tt></i></span> <dd> <p class="para"> Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client. When set to <b><tt>TRUE</tt></b>, the cookie will only be set if a secure connection exists. The default is <b><tt>FALSE</tt></b>. On the server-side, it's on the programmer to send this kind of cookie only on secure connection (e.g. with respect to <var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER["HTTPS"]</a></var>). </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">httponly</tt></i></span>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?