I was helping a friend get the login part of their site working – including the setting of user variable cookies. The cookies wouldn’t set.

Brick wall bang, bang, bang (my head you understand).

“I’m not sending anything to the browser before calling the cookies!” Frantic checking of all includes for anything happening.

Brick wall bang, bang, bang!

Never had this trouble before. Google. Add to code:

if (!setcookie("example","value")) {
echo "Cookie not set";

You’ve guessed it “Cookie not set” go to manual page for setcookie() and the magic words including whitespace!

The page source reveals:

Cookie not set

Now we know why this has never happened before – my friend is addicted to white space in their code. I remove any blank lines in mine. I removed the lot and the cookies are happy! Told the friend that if they didn’t want any cookies to set just to put lots of blank spaces back into their code!

Job done but sore head!