PHP cannot read a cookie set by JavaScript. Although the. PHP file is in a separate folderript file, they are on the same domain. When I set the cookie in JavaScript, I use fetch to call the PHP file. However, when the call is made, PHP cannot see any cookies. I can see the script running successfully in the network console.
Javascript:
document.cookie = "name=value0;";
fetch("/path/to/file.php", {
method: "POST",
credentials: "same-origin",
body: JSON.stringify({
value1: value1,
value2: value2
}),
headers: {
"Content-Type": "application/json"
},
});
PHP:
var_dump($_COOKIE);
PHP response:
array(0) {
}
fetchdoesn't send the document or know about your cookie. You should set your cookie in thefetchitself. Check this question/answer stackoverflow.com/questions/34815845/…Pathto the cookie