MrDi
MrDi

Reputation: 113

PHP session has wrong message

Does anyone know why this code gives false when it should give true?

setcookie("r", "", strtotime('2020-01-01'));
if (isset($_COOKIE["r"])) {
  echo "true";
}
else {
  echo "false";
}

Upvotes: 0

Views: 34

Answers (1)

John Conde
John Conde

Reputation: 219804

From the manual:

Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.

Basically you can't access a cookie you just set since cookies are only sent with each page request. So it actually doesn't exist yet.

Upvotes: 2

Related Questions