0

My code consists of a setcookie before any other code. I made it so that if setcookie returns false, the code will tell me.

Every time I run it, it returns false. What's wrong?

$l = setcookie('token', $token); if (!$l) { die("Cookie could not set"); }

Output: Cookie could not set

I have used sessions also, and every time I try the cookie for session id never sets.

Is my browser broken or am I broken?

  • Possible duplicate of [How to fix "Headers already sent" error in PHP](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Michel Sep 30 '18 at 08:28

1 Answers1

1

As the manual says:

If output exists prior to calling this function, setcookie() will fail and return FALSE.

This means that somewhere before you call setcookie, you are generating some output (perhaps from an echo, or some raw HTML). You need to put the call to setcookie before any other output is generated from your PHP file.

Nick
  • 138,499
  • 22
  • 57
  • 95
  • Nick is correct unfortunately. You've got a potential needle in a haystack quest ahead of you I fear. – trollboy Sep 30 '18 at 05:16
  • Right. I saw this, and eventually I just literally rewrote letter by letter the code into a new document and it worked. Thanks for putting me on the trail! – AdministratorReece Sep 30 '18 at 17:12