A
A
Aricus2020-06-05 05:57:46
PHP
Aricus, 2020-06-05 05:57:46

Why are cookies not being set?

If it matters, the wordpress site is XAMPP as the server.
Above functions.php I write

setcookie("test3", 1, time() + 7 * 86400);
var_dump($_COOKIE["test3"]);

and refresh the page several times. Writes null every time. In other words, no cookies are stored. Tried to write additional parameters, the result is the same. What could be the reason?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AUser0, 2020-06-05
@AUser0

Apparently , BEFORE calling setcookie (), you already have some kind of page output.
Because of this, the data (cookies) that should be displayed in the header of the HTTP response do not have time to be transmitted.
PHP, of course, complains about the late use of setcookie (), but you have suppressed the output of errors on the page, they are not visible. Look in the error.log of the WEB server, there will be these 'Cannot modify header information' errors...
But no, WP puts all the errors in its log file, look there. Or maybe it's WP rebuilding all the logic of work, and you need to use only its cookie management functions themselves ...
To summarize: move setcookie() to index.php as the first line, which would definitely work before any output.

T
tempick, 2020-06-05
@tempick

Here's a joke, that's what - cookies are set at the time of the server's response, and the value from $ _COOKIE is retrieved before the response. That is, despite the fact that in the code you first set cookies, and then output - in reality it happens in a different order. If you refresh the page again, you should see the value. If it still remains null, then AUser0 is right, and you really have some kind of output going on before you try to set the cookie. Here is just the same problem - cookies are transmitted along with the server's response to the browser. If you output something in response before calling setcookie (), then the cookies simply will not be transferred to the browser

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question