PHP setcookie() runs but the browser never keeps the cookie
setcookie() returns falseShort 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.
Fix it fast
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-cookie2Match what you see, then jump to its fix
- 1
setcookie() returns false and curl shows no Set-Cookie header.Output was sent before setcookie()Fix - 2
The header is present in curl, but $_COOKIE['name'] is empty right after the call.You read $_COOKIE in the same requestFix - 3
The 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
The 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
- 1
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 anotherThe 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 -32Fix
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-cookieConfirmed by php.net
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.txt2Fix
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 request3Verify
A second request with the cookie jar prints the value.
curl -s -b jar.txt https://example.com/read.phpConfirmed by php.net
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-cookie2Fix
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-cookieConfirmed by php.net
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" *.php2Fix
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-cookieConfirmed by php.net
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
Related fixes
Back end
Warning: Cannot modify header information - headers already sent
Warning: session_start(): Session cannot be started after headers have already been sent
PHP sent output to the browser before your header(), setcookie() or session_start() call, and headers must go first.
3 causes, each with a fixSecurity
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 fixBack end
Chrome shows net::ERR_INCOMPLETE_CHUNKED_ENCODING on a PHP page
The page loads partly, then the response stops
The browser got a chunked response that ended before its final zero-length chunk, so the connection closed mid-page.
2 causes, each with a fixBack 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 fixBack end
Laravel file upload returns 413 behind nginx
413 Content Too Large
A 413 means a limit refused the request body before Laravel could use it.
3 causes, each with a fixBack end
Laravel routes return 404 or throw NotFoundHttpException
Symfony\Component\HttpKernel\Exception\NotFoundHttpException
A 404 means Laravel found no route for that URL, or the request never reached Laravel at all.
3 causes, each with a fix