J
J
JackShcherbakov2018-02-04 21:28:20
PHP
JackShcherbakov, 2018-02-04 21:28:20

Why is the cookie not being read?

Why the code doesn't work

<?php
  setcookie("cookiewithdomain", "ralph", null, null, null, true, true);
  print $_COOKIE["cookiewithdomain"];
?>

The last argument makes the cookie inaccessible to JS but not PHP, what's the problem?
From these cookies, the head is already square XD
Thank you all!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Vlasov, 2018-02-04
@tr0yka

if(isset($_GET['q'])){
    setcookie("cookiewithdomain", "ralph", null, null, null, false, true);
}

print $_COOKIE["cookiewithdomain"];

A
Anatoly Kirsanov, 2018-02-04
@bitrixworkshop

Because setcookie sends a cookie header to the browser. In order for the cookie to arrive on the server (and the print line to work "correctly"), you need to turn to the page again.
Maxim Vlasov suggested separating the moment of creating and receiving cookies. On his script, refer to the page with the q parameter. This will set the cookie (send it to the browser). Then, without a parameter to it, the page will show the cookie received from the browser.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question