Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 5744

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Send a cookie</title>

 </head>
 <body><div class="manualnavbar" 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> &mdash; <span class="dc-title">Send a cookie</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.setcookie-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">bool</span> <span class="methodname"><strong>setcookie</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$name</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$value</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$expire</code><span class="initializer"> = 0</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$path</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$domain</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$secure</code><span class="initializer"> = false</span></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$httponly</code><span class="initializer"> = false</span></span>
  ]]]]]] )</div>

  <p class="para rdfs-comment">
    <span class="function"><strong>setcookie()</strong></span> 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 <em>&lt;html&gt;</em> and
   <em>&lt;head&gt;</em> 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"><var class="varname"><a href="reserved.variables.cookies.html" class="classname">$_COOKIE</a></var></var> or
   <var class="varname"><var class="varname">$HTTP_COOKIE_VARS</var></var> arrays.  Note,
   <a href="language.variables.superglobals.html" class="link">superglobals</a>
   such as <var class="varname"><var class="varname"><a href="reserved.variables.cookies.html" class="classname">$_COOKIE</a></var></var> became available in PHP 4.1.0.
   Cookie
   values also exist in <var class="varname"><var class="varname"><a href="reserved.variables.request.html" class="classname">$_REQUEST</a></var></var>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.setcookie-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   All the arguments except the <em><code class="parameter">name</code></em> argument are
   optional. You may also replace an argument with an empty string
   (<em class="emphasis">&quot;&quot;</em>) in order to skip that argument.
   Because the <em><code class="parameter">expire</code></em> argument is integer, it cannot
   be skipped with an empty string, use a zero (<em class="emphasis">0</em>)
   instead.
  </p>
  <p class="para">
   <a href="http://www.faqs.org/rfcs/rfc6265" class="link external">&raquo;&nbsp;RFC 6265</a> provides the normative
   reference on how each  <span class="function"><strong>setcookie()</strong></span> parameter is
   interpreted.
   <dl>

    <dt>

     <span class="term"><em><code class="parameter">name</code></em></span>
     <dd>

      <p class="para">
       The name of the cookie.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">value</code></em></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
       <em><code class="parameter">name</code></em> is <em>&#039;cookiename&#039;</em>, this
       value is retrieved through <var class="varname"><var class="varname"><a href="reserved.variables.cookies.html" class="classname">$_COOKIE['cookiename']</a></var></var>
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">expire</code></em></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&#039;ll
       most likely set this with the  <span class="function"><a href="function.time.html" class="function">time()</a></span> function
       plus the number of seconds before you want it to expire.  Or
       you might use  <span class="function"><a href="function.mktime.html" class="function">mktime()</a></span>.
       <em>time()+60*60*24*30</em> 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 class="note"><p><strong class="note">Note</strong>: 
        <p class="para">
         You may notice the <em><code class="parameter">expire</code></em> parameter takes on a
         Unix timestamp, as opposed to the date format <em>Wdy, DD-Mon-YYYY
         HH:MM:SS GMT</em>, this is because PHP does this conversion
         internally.
        </p>
       </p></blockquote>
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">path</code></em></span>
     <dd>

      <p class="para">
       The path on the server in which the cookie will be available on.
       If set to <em>&#039;/&#039;</em>, the cookie will be available
       within the entire <em><code class="parameter">domain</code></em>.  If set to
       <em>&#039;/foo/&#039;</em>, the cookie will only be available
       within the <em>/foo/</em> directory and all
       sub-directories such as <em>/foo/bar/</em> of
       <em><code class="parameter">domain</code></em>.  The default value is the
       current directory that the cookie is being set in.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">domain</code></em></span>
     <dd>

      <p class="para">
       The domain that the cookie is available to. Setting the domain to
       <em>&#039;www.example.com&#039;</em> will make the cookie
       available in the <em>www</em> subdomain and higher subdomains.
       Cookies available to a lower domain, such as
       <em>&#039;example.com&#039;</em> will be available to higher subdomains,
       such as <em>&#039;www.example.com&#039;</em>.
       
       Older browsers still implementing the deprecated
       <a href="http://www.faqs.org/rfcs/rfc2109" class="link external">&raquo;&nbsp;RFC 2109</a> may require a leading
       <em>.</em> to match all subdomains.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">secure</code></em></span>
     <dd>

      <p class="para">
       Indicates that the cookie should only be transmitted over a
       secure HTTPS connection from the client. When set to <strong><code>TRUE</code></strong>, the
       cookie will only be set if a secure connection exists.
       On the server-side, it&#039;s on the programmer to send this
       kind of cookie only on secure connection (e.g. with respect to
       <var class="varname"><var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER["HTTPS"]</a></var></var>).
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">httponly</code></em></span>
     <dd>

      <p class="para">
       When <strong><code>TRUE</code></strong> 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. It has been suggested that
       this setting can effectively help to reduce identity theft through
       XSS attacks (although it is not supported by all browsers), but that
       claim is often disputed. Added in PHP 5.2.0.
       <strong><code>TRUE</code></strong> or <strong><code>FALSE</code></strong>
      </p>
     </dd>

    </dt>

   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.setcookie-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   If output exists prior to calling this function,
    <span class="function"><strong>setcookie()</strong></span> will fail and return <strong><code>FALSE</code></strong>. If
    <span class="function"><strong>setcookie()</strong></span> successfully runs, it will return <strong><code>TRUE</code></strong>.
   This does not indicate whether the user accepted the cookie.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.setcookie-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   Some examples follow how to send cookies:
   <div class="example" id="example-4493">
    <p><strong>Example #1  <span class="function"><strong>setcookie()</strong></span> send example</strong></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
    <span class="function"><a href="function.setrawcookie.html" class="function">setrawcookie()</a></span> 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" id="example-4494">
    <p><strong>Example #2  <span class="function"><strong>setcookie()</strong></span> delete example</strong></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" id="example-4495">
    <p><strong>Example #3  <span class="function"><strong>setcookie()</strong></span> and arrays</strong></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;</span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$name</span><span style="color: #DD0000">&nbsp;:&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #DD0000">&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 screen">
<div class="cdata"><pre>
three : cookiethree
two : cookietwo
one : cookieone
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.setcookie-changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.5.0</td>
       <td>
        A Max-Age attribute is now included in the Set-Cookie header sent to
        the client.
       </td>
      </tr>

      <tr>
       <td>5.2.0</td>
       <td>
        The <em><code class="parameter">httponly</code></em> parameter was added.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.setcookie-notes">
  <h3 class="title">Notes</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    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  <span class="function"><a href="function.ob-start.html" class="function">ob_start()</a></span> and
     <span class="function"><a href="function.ob-end-flush.html" class="function">ob_end_flush()</a></span> in your script, or setting the
    <em>output_buffering</em> configuration directive on in your
    <var class="filename">php.ini</var> or server configuration files.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    If the PHP directive <a href="ini.core.html#ini.register-globals" class="link">register_globals</a>
    is set to <em>on</em> then cookie values will also be made into
    variables.  In our examples below, <var class="varname"><var class="varname">$TestCookie</var></var> will
    exist.  It&#039;s recommended to use <var class="varname"><var class="varname"><a href="reserved.variables.cookies.html" class="classname">$_COOKIE</a></var></var>.
   </p>
  </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 <em><code class="parameter">expire</code></em>
      parameter.  A nice way to debug the existence of cookies is by
      simply calling <em>print_r($_COOKIE);</em>.
     </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 <strong><code>FALSE</code></strong>, and all other arguments
      match a previous call to setcookie, then the cookie with the specified
      name will be deleted from the remote client.
      This is internally achieved by setting value to &#039;deleted&#039; and expiration
      time to one year in past.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      Because setting a cookie with a value of <strong><code>FALSE</code></strong> will try to delete the cookie,
      you should not use boolean values. Instead, use <em class="emphasis">0</em> for <strong><code>FALSE</code></strong>
      and <em class="emphasis">1</em> for <strong><code>TRUE</code></strong>.
     </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 user&#039;s
      system. Consider  <span class="function"><a href="function.explode.html" class="function">explode()</a></span> to set one cookie with
      multiple names and values. It is not recommended to use
       <span class="function"><a href="function.serialize.html" class="function">serialize()</a></span> for this purpose, because it can result
      in security holes.
     </span>
    </li>
   </ul>
  </p>
  <p class="simpara">
   Multiple calls to  <span class="function"><strong>setcookie()</strong></span> are performed in the order called.
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.setcookie-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"> <span class="function"><a href="function.header.html" class="function" rel="rdfs-seeAlso">header()</a> - Send a raw HTTP header</span></li>
    <li class="member"> <span class="function"><a href="function.setrawcookie.html" class="function" rel="rdfs-seeAlso">setrawcookie()</a> - Send a cookie without urlencoding the cookie value</span></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/rfc6265" class="link external">&raquo;&nbsp;RFC 6265</a></li>
    <li class="member"><a href="http://www.faqs.org/rfcs/rfc2109" class="link external">&raquo;&nbsp;RFC 2109</a></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" 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>