<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.network.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'function.setcookie.php',
    1 => 'setcookie',
    2 => 'Send a cookie',
  ),
  'up' => 
  array (
    0 => 'ref.network.php',
    1 => 'Функції для мережі',
  ),
  'prev' => 
  array (
    0 => 'function.request-parse-body.php',
    1 => 'request_parse_body',
  ),
  'next' => 
  array (
    0 => 'function.setrawcookie.php',
    1 => 'setrawcookie',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/network/functions/setcookie.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.setcookie" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">setcookie</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</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">Опис</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>setcookie</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$name</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$value</code><span class="initializer"> = &quot;&quot;</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$expires_or_options</code><span class="initializer"> = 0</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$path</code><span class="initializer"> = &quot;&quot;</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$domain</code><span class="initializer"> = &quot;&quot;</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$secure</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$httponly</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong></span></span><br>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="para rdfs-comment">Alternative signature available as of PHP 7.3.0 (not supported with named parameters):</p>
  <div class="methodsynopsis dc-description"><span class="methodname"><strong>setcookie</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$name</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$value</code><span class="initializer"> = &quot;&quot;</span></span>, <span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$options</code><span class="initializer"> = []</span></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></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>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 <code class="literal">&lt;html&gt;</code> and
   <code class="literal">&lt;head&gt;</code> 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.php" class="classname">$_COOKIE</a></var> array.
   Cookie
   values may also exist in <var class="varname"><a href="reserved.variables.request.php" class="classname">$_REQUEST</a></var>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.setcookie-parameters">
  <h3 class="title">Параметри</h3>
  <p class="para">
   <a href="https://datatracker.ietf.org/doc/html/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><code class="parameter">name</code></dt>
     <dd>
      <p class="para">
       The name of the cookie.
      </p>
     </dd>
    
    
     <dt><code class="parameter">value</code></dt>
     <dd>
      <p class="para">
       The value of the cookie.  This value is stored on the clients computer;
       do not store sensitive information.  Assuming the
       <code class="parameter">name</code> is <code class="literal">&#039;cookiename&#039;</code>, this
       value is retrieved through <var class="varname"><a href="reserved.variables.cookies.php" class="classname">$_COOKIE['cookiename']</a></var>
      </p>
     </dd>
    
    
     <dt><code class="parameter">expires_or_options</code></dt>
     <dd>
      <p class="para">
       The time the cookie expires.  This is a Unix timestamp so is
       in number of seconds since the epoch.
       One way to set this is by adding the number of seconds before the cookie
       should expire to the result of calling <span class="function"><a href="function.time.php" class="function">time()</a></span>.
       For instance, <code class="literal">time()+60*60*24*30</code> will set the cookie to
       expire in 30 days.
       Another option is to use the <span class="function"><a href="function.mktime.php" class="function">mktime()</a></span> function.
       If set to <code class="literal">0</code>, 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">Зауваження</strong>: 
        <p class="para">
         You may notice the <code class="parameter">expires_or_options</code> parameter takes on a
         Unix timestamp, as opposed to the date format <code class="literal">Wdy, DD-Mon-YYYY
         HH:MM:SS GMT</code>, this is because PHP does this conversion
         internally.
        </p>
       </p></blockquote>
      </p>
     </dd>
    
    
     <dt><code class="parameter">path</code></dt>
     <dd>
      <p class="para">
       The path on the server in which the cookie will be available on.
       If set to <code class="literal">&#039;/&#039;</code>, the cookie will be available
       within the entire <code class="parameter">domain</code>.  If set to
       <code class="literal">&#039;/foo/&#039;</code>, the cookie will only be available
       within the <code class="literal">/foo/</code> directory and all
       sub-directories such as <code class="literal">/foo/bar/</code> of
       <code class="parameter">domain</code>.  The default value is the
       current directory that the cookie is being set in.
      </p>
     </dd>
    
    
     <dt><code class="parameter">domain</code></dt>
     <dd>
      <p class="para">
       The (sub)domain that the cookie is available to. Setting this to a
       subdomain (such as <code class="literal">&#039;www.example.com&#039;</code>) will make the
       cookie available to that subdomain and all other sub-domains of it (i.e.
       w2.www.example.com). To make the cookie available to the whole domain
       (including all subdomains of it), simply set the value to the domain
       name (<code class="literal">&#039;example.com&#039;</code>, in this case).
      </p>
      <p class="para">
       Older browsers still implementing the deprecated
       <a href="https://datatracker.ietf.org/doc/html/rfc2109" class="link external">&raquo;&nbsp;RFC 2109</a> may require a leading
       <code class="literal">.</code> to match all subdomains.
      </p>
     </dd>
    
    
     <dt><code class="parameter">secure</code></dt>
     <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><a href="reserved.constants.php#constant.true">true</a></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"><a href="reserved.variables.server.php" class="classname">$_SERVER["HTTPS"]</a></var>).
      </p>
     </dd>
    
    
     <dt><code class="parameter">httponly</code></dt>
     <dd>
      <p class="para">
       When <strong><code><a href="reserved.constants.php#constant.true">true</a></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.
       <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>
      </p>
     </dd>
    
    
     <dt><code class="parameter">options</code></dt>
     <dd>
      <p class="para">
       An associative <span class="type"><a href="language.types.array.php" class="type array">array</a></span> which may have any of the keys
       <code class="literal">expires</code>, <code class="literal">path</code>, <code class="literal">domain</code>,
       <code class="literal">secure</code>, <code class="literal">httponly</code> and <code class="literal">samesite</code>.
       If any other key is present an error of level <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>
       is generated. The values have the same meaning as described for the 
       parameters with the same name. The value of the <code class="literal">samesite</code>
       element should be either <code class="literal">None</code>, <code class="literal">Lax</code>
       or <code class="literal">Strict</code>.
       If any of the allowed options are not given, their default values are the
       same as the default values of the explicit parameters. If the
       <code class="literal">samesite</code> element is omitted, no SameSite cookie
       attribute is set.
      </p>
      <p class="para">
       <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
        <p class="para">
         To set a cookie that includes attributes that aren&#039;t among the keys listed,
         use <span class="function"><a href="function.header.php" class="function">header()</a></span>.
        </p>
       </p></blockquote>
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.setcookie-returnvalues">
  <h3 class="title">Значення, що повертаються</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><a href="reserved.constants.php#constant.false">false</a></code></strong>. If
   <span class="function"><strong>setcookie()</strong></span> successfully runs, it will return <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>.
   This does not indicate whether the user accepted the cookie.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.setcookie-changelog">
  <h3 class="title">Журнал змін</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Версія</th>
       <th>Опис</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.2.0</td>
       <td>
        The date format of the cookie is now <code class="literal">&#039;D, d M Y H:i:s \G\M\T&#039;</code>;
        previously it was <code class="literal">&#039;D, d-M-Y H:i:s T&#039;</code>.
       </td>
      </tr>

      <tr>
       <td>7.3.0</td>
       <td>
        An alternative signature supporting an <code class="parameter">options</code>
        array has been added. This signature supports also setting of the
        SameSite cookie attribute.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.setcookie-examples">
  <h3 class="title">Приклади</h3>
  <p class="para">
   The following examples demonstrate some ways to send cookies.
   <div class="example" id="example-1">
    <p><strong>Приклад #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 /><br />$value </span><span style="color: #007700">= </span><span style="color: #DD0000">'something from 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">, </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">, </span><span style="color: #0000BB">$value</span><span style="color: #007700">, </span><span style="color: #0000BB">time</span><span style="color: #007700">()+</span><span style="color: #0000BB">3600</span><span style="color: #007700">);  </span><span style="color: #FF8000">/* expire in 1 hour */<br /></span><span style="color: #0000BB">setcookie</span><span style="color: #007700">(</span><span style="color: #DD0000">"TestCookie"</span><span style="color: #007700">, </span><span style="color: #0000BB">$value</span><span style="color: #007700">, </span><span style="color: #0000BB">time</span><span style="color: #007700">()+</span><span style="color: #0000BB">3600</span><span style="color: #007700">, </span><span style="color: #DD0000">"/~rasmus/"</span><span style="color: #007700">, </span><span style="color: #DD0000">"example.com"</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /><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.php" class="function">setrawcookie()</a></span> instead. 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">// Print an individual cookie<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$_COOKIE</span><span style="color: #007700">[</span><span style="color: #DD0000">"TestCookie"</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">// Another way to debug/test is to view all 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-2">
    <p><strong>Приклад #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">// set the expiration date to one hour ago<br /></span><span style="color: #0000BB">setcookie</span><span style="color: #007700">(</span><span style="color: #DD0000">"TestCookie"</span><span style="color: #007700">, </span><span style="color: #DD0000">""</span><span style="color: #007700">, </span><span style="color: #0000BB">time</span><span style="color: #007700">() - </span><span style="color: #0000BB">3600</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">, </span><span style="color: #DD0000">""</span><span style="color: #007700">, </span><span style="color: #0000BB">time</span><span style="color: #007700">() - </span><span style="color: #0000BB">3600</span><span style="color: #007700">, </span><span style="color: #DD0000">"/~rasmus/"</span><span style="color: #007700">, </span><span style="color: #DD0000">"example.com"</span><span style="color: #007700">, </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-3">
    <p><strong>Приклад #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">// set the 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">, </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">, </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">, </span><span style="color: #DD0000">"cookieone"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// after the page reloads, print them out<br /></span><span style="color: #007700">if (isset(</span><span style="color: #0000BB">$_COOKIE</span><span style="color: #007700">[</span><span style="color: #DD0000">'cookie'</span><span style="color: #007700">])) {<br />    foreach (</span><span style="color: #0000BB">$_COOKIE</span><span style="color: #007700">[</span><span style="color: #DD0000">'cookie'</span><span style="color: #007700">] as </span><span style="color: #0000BB">$name </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br />        </span><span style="color: #0000BB">$name </span><span style="color: #007700">= </span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">);<br />        </span><span style="color: #0000BB">$value </span><span style="color: #007700">= </span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">);<br />        echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$name</span><span style="color: #DD0000"> : </span><span style="color: #0000BB">$value</span><span style="color: #DD0000"> &lt;br /&gt;\n"</span><span style="color: #007700">;<br />    }<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
    <div class="example-contents screen">
     <div class="examplescode"><pre class="examplescode">three : cookiethree
two : cookietwo
one : cookieone</pre>
</div>
    </div>
   </div>
   <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
    <span class="simpara">
     Using separator characters such as <code class="literal">[</code> and <code class="literal">]</code>
     as part of the cookie name is not compliant to RFC 6265, section 4, but supposed
     to be supported by user agents according to RFC 6265, section 5.
    </span>
   </p></blockquote>
  </p>
 </div>

 
 <div class="refsect1 notes" id="refsect1-function.setcookie-notes">
  <h3 class="title">Примітки</h3>
  <blockquote class="note"><p><strong class="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.php" class="function">ob_start()</a></span> and
    <span class="function"><a href="function.ob-end-flush.php" class="function">ob_end_flush()</a></span> in your script, or setting the
    <code class="literal">output_buffering</code> configuration directive on in your
    <var class="filename">php.ini</var> or server configuration files.
   </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 <code class="parameter">expires_or_options</code>
      parameter.  A nice way to debug the existence of cookies is by
      simply calling <code class="literal">print_r($_COOKIE);</code>.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      Cookies must be deleted with the same parameters as they were set with.
      If the <code class="parameter">value</code> argument is an empty string, and all other arguments
      match a previous call to <span class="function"><strong>setcookie()</strong></span>, then the cookie with the specified
      name will be deleted from the remote client.
      This is internally achieved by setting value to <code class="literal">&#039;deleted&#039;</code> and expiration
      time in the past.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      Because setting a cookie with a value of <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> will try to delete the cookie,
      you should not use boolean values. Instead, use <em>0</em> for <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>
      and <em>1</em> for <strong><code><a href="reserved.constants.php#constant.true">true</a></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.php" 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.php" 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">Прогляньте також</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.header.php" class="function" rel="rdfs-seeAlso">header()</a> - Send a raw HTTP header</span></li>
    <li><span class="function"><a href="function.setrawcookie.php" class="function" rel="rdfs-seeAlso">setrawcookie()</a> - Send a cookie without urlencoding the cookie value</span></li>
    <li><a href="features.cookies.php" class="link">cookies section</a></li>
    <li><a href="https://datatracker.ietf.org/doc/html/rfc6265" class="link external">&raquo;&nbsp;RFC 6265</a></li>
    <li><a href="https://datatracker.ietf.org/doc/html/rfc2109" class="link external">&raquo;&nbsp;RFC 2109</a></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>