PHP setcookie() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, setcookie() queues a Set-Cookie header so the browser stores a small name/value pair and sends it back on later requests.

You must call it before any page output. Classic arguments cover expires, path, domain, secure, and httponly. From PHP 7.3 you can pass an options array instead, which also supports samesite. A true return only means the header was prepared, not that the user accepted the cookie.

Syntax

Call setcookie() with the values you need. Start with this line.

PHP
setcookie('theme', 'dark', ['expires' => time() + 3600, 'path' => '/', 'httponly' => true]);
CallWhat it does
setcookie('theme', 'dark')Sets a session cookie that lasts until the browser closes.
setcookie('id', '42', time() + 86400, '/')Classic form: expire in one day, available on the whole site.
setcookie('id', '42', ['expires' => time() + 86400, 'path' => '/', 'secure' => true, 'httponly' => true, 'samesite' => 'Lax'])PHP 7.3+ options array with secure, HttpOnly, and SameSite.

Call setcookie() before echo, HTML, or whitespace. To delete a cookie, set it again with an expires time in the past and the same path/domain. Values are urlencoded automatically; use setrawcookie() when you must send a raw value.

Library declarations
PHP
setcookie(string $name, string $value = "", array|int $expires_or_options = 0, string $path = "", string $domain = "", bool $secure = false, bool $httponly = false): bool

Parameters

ParameterDescription
nameRequired. Cookie name.
valueOptional. Cookie value. Do not store secrets here.
expires_or_optionsOptional. Unix timestamp expiry, or from PHP 7.3 an options array with expires, path, domain, secure, httponly, and samesite.
pathOptional in the classic form. Path where the cookie is available, often /.
domainOptional in the classic form. Host or domain that should receive the cookie.
secureOptional in the classic form. true to send the cookie only over HTTPS.
httponlyOptional in the classic form. true to hide the cookie from JavaScript.

Return Value

Return ValueDescription
trueThe Set-Cookie header was successfully queued.
falseThe cookie could not be set, often because output already started.

PHP Examples

Example 1 — Set a Simple Cookie

Queue a cookie with the options array and confirm the function returned true.

PHP
<?php
$ok = setcookie('theme', 'dark', [
    'path' => '/',
    'httponly' => true,
]);

var_dump($ok);

How It Works

  1. The cookie name is theme and the value is dark.
  2. path / makes it available for the whole site.
  3. httponly reduces access from JavaScript.
  4. true means the header was queued successfully.

Example 2 — Classic expires, path, and domain Arguments

Use the older argument list with an expiry timestamp and a site-wide path.

PHP
<?php
$expires = time() + 3600;
$ok = setcookie('lang', 'en', $expires, '/', '', false, true);
echo $ok ? 'cookie queued' : 'cookie failed', PHP_EOL;
echo 'expires in about one hour', PHP_EOL;

How It Works

  1. time() + 3600 sets expiry one hour from now.
  2. path / covers the whole site.
  3. An empty domain string keeps the current host default.
  4. The last true turns on HttpOnly in the classic signature.

Example 3 — Inspect the Queued Set-Cookie Header

After setcookie(), scan headers_list() for the queued cookie line.

PHP
<?php
setcookie('sid', 'abc123', [
    'path' => '/',
    'secure' => true,
    'httponly' => true,
    'samesite' => 'Lax',
]);

foreach (headers_list() as $header) {
    if (stripos($header, 'Set-Cookie:') === 0) {
        echo $header, PHP_EOL;
    }
}

How It Works

  1. The options array sets path, secure, httponly, and samesite together.
  2. headers_list() shows headers PHP has prepared.
  3. We print only the Set-Cookie lines.
  4. SameSite=Lax is available through the PHP 7. 3+ array form.

Frequently Asked Questions

It sends a Set-Cookie header so the browser can store a cookie and return it on later requests.
Before any output is sent. Like header(), it fails if the response body has already started.
From PHP 7.3 you can pass expires, path, domain, secure, httponly, and samesite in one associative array as the third argument.
No. $_COOKIE shows cookies the browser already sent. The new value appears on the next request after the browser stores it.
Call setcookie() again with the same name, path, and domain, and an expires time in the past, often with an empty value.

Next: setrawcookie()

setrawcookie() queues a Set-Cookie header just like setcookie(), except the cookie value i.

setrawcookie() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful