Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP setcookie() runs but the browser never keeps the cookie

Errorsetcookie() returns false

Short answer

setcookie() sends a Set-Cookie header, so it fails if any output was already sent. When it returns true, the cookie still depends on path, domain, secure and samesite matching the page that reads it, and $_COOKIE only shows it on the next request. Look at the raw Set-Cookie header first to see which case you have.

  • Applies to PHP 8.3, PHP 8.4, Apache or nginx with PHP-FPM
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask the server directly whether it sends a Set-Cookie header. This tells you if the problem is in PHP or in how the browser treats the cookie.

    curl -s -D - -o /dev/null https://example.com/login.php | grep -i set-cookie
  2. 2Match what you see, then jump to its fix

    1. 1setcookie() returns false and curl shows no Set-Cookie header.Output was sent before setcookie()Fix
    2. 2The header is present in curl, but $_COOKIE['name'] is empty right after the call.You read $_COOKIE in the same requestFix
    3. 3The cookie appears for one URL and not for another, or only over https.Path, domain or secure do not match the page that reads the cookieFix
    4. 4The cookie is set and then disappears, or never appears when the value is false.A boolean false value, or a call that deletes the cookieFix

What does the error look like?

  • setcookie() returns false
  • $_COOKIE is empty right after calling setcookie()
  • The cookie works on one page and is missing on another
  • The cookie is rejected by the browser when samesite is None

Cause 1 of 4

Output was sent before setcookie()

1Check

The manual says cookies must be sent before any output, including html tags and whitespace. If output exists, setcookie() fails and returns false. Look for echo, HTML above the PHP tag, a space before <?php, or a BOM in an included file.

php -l login.php && head -c 20 login.php | xxd | head -3

2Fix

Call setcookie() before any output, or turn on output buffering with ob_start() at the top, or set output_buffering in php.ini.

<?php
ob_start();
setcookie('theme', 'dark', ['expires' => time() + 3600, 'path' => '/']);

3Verify

The response now includes a Set-Cookie header.

curl -s -D - -o /dev/null https://example.com/login.php | grep -i set-cookie

Confirmed by php.net

Did this fix it?

Cause 2 of 4

You read $_COOKIE in the same request

1Check

The manual says cookies become visible only on the next load of a page the cookie applies to. The array holds what the browser sent with this request.

curl -s -c jar.txt -b jar.txt https://example.com/login.php && cat jar.txt

2Fix

Use the value you just set from your own variable in this request, and read $_COOKIE on the following one.

$theme = 'dark';
setcookie('theme', $theme, ['path' => '/']);
// use $theme now; $_COOKIE['theme'] exists on the next request

3Verify

A second request with the cookie jar prints the value.

curl -s -b jar.txt https://example.com/read.php

Confirmed by php.net

Did this fix it?

Cause 3 of 4

Path, domain or secure do not match the page that reads the cookie

1Check

The default path is the directory of the script that set it, so a cookie set in /account/ is not sent to /. A secure cookie is only set over a secure connection.

curl -s -D - -o /dev/null https://example.com/account/login.php | grep -i set-cookie

2Fix

Set path to / for site-wide use, and use the options array. If samesite is None, secure must also be true or the client blocks the cookie.

setcookie('session_hint', 'abc', [
    'expires'  => time() + 86400,
    'path'     => '/',
    'secure'   => true,
    'httponly' => true,
    'samesite' => 'Lax',
]);

3Verify

The Set-Cookie header shows Path=/ and the cookie is sent back on other pages.

curl -s -D - -o /dev/null https://example.com/account/login.php | grep -i set-cookie

Confirmed by php.net

Did this fix it?

Cause 4 of 4

A boolean false value, or a call that deletes the cookie

1Check

A value of false tries to delete the cookie. An empty value with the same other arguments as an earlier call also deletes it.

grep -n "setcookie" *.php

2Fix

Store 0 or 1 instead of a boolean. When you delete, use the same path and domain as when you set it.

setcookie('consent', '1', ['path' => '/']);
// to delete
setcookie('consent', '', time() - 3600, '/');

3Verify

The first call sets the cookie and the second one removes it.

curl -s -D - -o /dev/null https://example.com/consent.php | grep -i set-cookie

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Keep setcookie() calls near the top of the script, before any template output.
  • Use the options array form, so path, secure and samesite are always explicit.
  • Test cookies with curl -D - so you see the real header, not what the browser tool shows.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Security

The browser warns about a SameSite cookie set from PHP

A cookie with SameSite=None is rejected because it lacks the Secure attribute

A cookie sent without a SameSite attribute gets the browser's default, and a cookie marked None is dropped unless it is also Secure.

4 causes, each with a fix

Back end

CodeIgniter works locally but every page is 404 on the live server

The requested URL was not found on this server.

A 404 on the live server comes from one of four places: the document root is not the public folder, Apache ignores .htaccess or nginx has no try_files rule, the route is not defined because auto routing is off, or the controller file name differs from its class in case.

5 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.