Home PHP Network setcookie() PHP setcookie() Function 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.
setcookie('theme', 'dark', ['expires' => time() + 3600, 'path' => '/', 'httponly' => true]); Call What 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 setcookie(string $name, string $value = "", array|int $expires_or_options = 0, string $path = "", string $domain = "", bool $secure = false, bool $httponly = false): bool Parameters Parameter Description name Required. Cookie name. value Optional. Cookie value. Do not store secrets here. expires_or_options Optional. Unix timestamp expiry, or from PHP 7.3 an options array with expires, path, domain, secure, httponly, and samesite. path Optional in the classic form. Path where the cookie is available, often /. domain Optional in the classic form. Host or domain that should receive the cookie. secure Optional in the classic form. true to send the cookie only over HTTPS. httponly Optional in the classic form. true to hide the cookie from JavaScript.
Return Value Return Value Description true The Set-Cookie header was successfully queued. false The cookie could not be set, often because output already started.
Important
Cookies are visible to the client. Prefer httponly for session ids, secure on HTTPS sites, and a SameSite value in the options array on PHP 7.3+. setcookie() does not change $_COOKIE in the current request.
PHP Examples 1. Set a Simple Cookie 2. Classic expires, path, and domain Arguments 3. Inspect the Queued Set-Cookie Header Example 1 — Set a Simple Cookie Queue a cookie with the options array and confirm the function returned true.
<?php
$ok = setcookie('theme', 'dark', [
'path' => '/',
'httponly' => true,
]);
var_dump($ok); How It Works The cookie name is theme and the value is dark. path / makes it available for the whole site. httponly reduces access from JavaScript. 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
$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; cookie queued
expires in about one hour How It Works time() + 3600 sets expiry one hour from now. path / covers the whole site. An empty domain string keeps the current host default. The last true turns on HttpOnly in the classic signature. Frequently Asked Questions What does setcookie() do in PHP? It sends a Set-Cookie header so the browser can store a cookie and return it on later requests.
When must I call setcookie()? Before any output is sent. Like header(), it fails if the response body has already started.
What is the options array form? From PHP 7.3 you can pass expires, path, domain, secure, httponly, and samesite in one associative array as the third argument.
Does setcookie() update $_COOKIE immediately? No. $_COOKIE shows cookies the browser already sent. The new value appears on the next request after the browser stores it.
How do I delete a cookie? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion