PHP cannot see a cookie that JavaScript wrote with document.cookie
Your error
isset($_COOKIE['cart_id']) is false, yet the browser's storage panel lists the cookie2 more lines it can showFix it fast
Most common cause, 1 of 4document.cookie wrote the cookie without path=/
Write the path yourself, and keep the same path whenever you later change or delete that cookie, so you work on the same entry.
document.cookie = "cart_id=" + encodeURIComponent(cartId) +
"; path=/; max-age=2592000; secure; samesite=lax";Short answer
A cookie written with document.cookie only reaches PHP when the browser sends it with a later request whose path and host match. Without path=/ it stays in the page's folder, without domain= it skips subdomains, and PHP rewrites dots in names and url-decodes values. Write path=/, a plain name and an encoded value, then read it next request.
Which cause do you have?
Put a small script next to the PHP page that reads the cookie and open it in the same browser. If the cookie is missing from the raw Cookie header, the browser never sent it (path, timing or host). If it is in the header but looks different in $_COOKIE, PHP changed its name or value.
<?php
// cookie-debug.php
header('Content-Type: text/plain');
var_dump($_SERVER['HTTP_COOKIE'] ?? '(no Cookie header)');
var_dump($_COOKIE);Then find the line you see in this list and open its fix:
- 1document.cookie wrote the cookie without path=/
PHP pages in the folder where the script ran get the cookie; pages at the site root or under /api/ do not - 2PHP looks for the cookie during the request that created it
The cookie is set by JavaScript on page load, and the PHP code that checks it belongs to that same page - 3The PHP code lives on a different host or subdomain
JavaScript on www.example.com sets the cookie, PHP on api.example.com never receives it - 4PHP renamed the cookie or decoded its value on arrival
The raw header carries user.theme=dark or a base64 value with plus signs, while $_COOKIE shows user_theme or spaces in the value
What does the error look like?
isset($_COOKIE['cart_id']) is false, yet the browser's storage panel lists the cookieThe cookie reaches PHP from one folder of the site but not from anotherJavaScript wrote user.theme, and PHP only has user_theme
Cause 1 of 4
document.cookie wrote the cookie without path=/
You seePHP pages in the folder where the script ran get the cookie; pages at the site root or under /api/ do not
Check
A cookie from document.cookie is missing in $_COOKIE on other folders when the write had no path=, because the browser then limits the cookie to the folder of the page URL. RFC 6265 calls this the default path: the directory of the URL up to its last slash, so a cookie written on /shop/cart.html is only sent to addresses under /shop. The Path column in the browser's cookie storage panel shows which one it got.
Fix
Write the path yourself, and keep the same path whenever you later change or delete that cookie, so you work on the same entry.
document.cookie = "cart_id=" + encodeURIComponent(cartId) +
"; path=/; max-age=2592000; secure; samesite=lax";Verify
Copy cookie-debug.php to the site root and open it there: cart_id now shows in both the raw header and $_COOKIE.
Confirmed by rfc-editor.org
Cause 2 of 4
PHP looks for the cookie during the request that created it
You seeThe cookie is set by JavaScript on page load, and the PHP code that checks it belongs to that same page
Check
$_COOKIE is empty for a cookie your script has just created because PHP built the current page before the browser ran the script. A cookie travels to the server in the Cookie header of a request, so it can only reach PHP with a later one: the next page view, a form post or a fetch call. PHP's own manual gives the same rule for setcookie().
Fix
Read the cookie on the following request. When the server needs the value immediately, send it in the body or query string of a call to the server instead of relying on a cookie.
fetch('/save-timezone.php', {
method: 'POST',
body: new URLSearchParams({ tz: Intl.DateTimeFormat().resolvedOptions().timeZone })
});Verify
Reload the page or call cookie-debug.php after the script has run; the cookie is in $_COOKIE from then on.
Confirmed by php.net
Cause 3 of 4
The PHP code lives on a different host or subdomain
You seeJavaScript on www.example.com sets the cookie, PHP on api.example.com never receives it
Check
PHP on another subdomain never sees a cookie that document.cookie wrote without domain=, since that cookie belongs to the exact host of the page and is not shared with its subdomains. A domain value that does not match the page's own site is ignored without any error.
Fix
Set domain= to the shared parent domain so every subdomain receives the cookie.
document.cookie = "cart_id=" + encodeURIComponent(cartId) +
"; domain=example.com; path=/; secure; samesite=lax";Verify
cookie-debug.php on the other host now lists cart_id in the raw header.
Confirmed by developer.mozilla.org
Cause 4 of 4
PHP renamed the cookie or decoded its value on arrival
You seeThe raw header carries user.theme=dark or a base64 value with plus signs, while $_COOKIE shows user_theme or spaces in the value
Check
When the raw Cookie header has the cookie but $_COOKIE shows another key or value, PHP changed it on the way in. PHP replaces dots and spaces in incoming variable names, cookies included, with underscores, and a name with square brackets such as prefs[color] becomes an array. Cookie values are url-decoded as well, which turns %xx sequences into characters and a plus sign into a space.
<?php
var_dump(array_keys($_COOKIE));
var_dump(urldecode('ab+cd%3D'));Fix
Give cookies that PHP must read names made only of letters, digits and underscores, and wrap every value in encodeURIComponent() in JavaScript. In PHP, use the value from $_COOKIE as it is; decoding it a second time can damage it again.
document.cookie = "user_theme=dark; path=/; samesite=lax";
document.cookie = "prefs=" + encodeURIComponent(JSON.stringify(prefs)) + "; path=/";
<?php
$theme = $_COOKIE['user_theme'] ?? 'light';
$prefs = json_decode($_COOKIE['prefs'] ?? '{}', true);Verify
array_keys($_COOKIE) lists exactly the names the JavaScript uses, and var_dump($prefs) prints the structure the script stored.
Confirmed by php.net
How do I stop it coming back?
- Write path= (normally /) on every document.cookie call, with the same path for updates and deletes.
- Keep cookie names that PHP reads to letters, digits and underscores.
- Wrap every value in encodeURIComponent() and never url-decode $_COOKIE values again in PHP.
- Data the server needs on this very request goes in the request itself, not in a fresh cookie.
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 on the same stack: stegnor.com, Spotless Laundry, EZ Estimater.