Adam Brown
Adam Brown

Reputation: 2870

Unable to setcookie in any browser

So I have done a lot of reading on setting cookies and still cannot find what I am doing wrong here. I started with an if statement to include another script but it soon became clear the cookie wasn't setting so I simplified it and made it just to echo so I could see if it was setting. Its not. I am including it above the <html>.

I am setting the cookie and then refreshing and no matter how many times I refresh it is still returning a NULL result in the echo command.

echo returning NULL and var_dump returning Array(0)

I am going mad, is there something wrong in my code, what am I missing?

Upvotes: 3

Views: 1405

Answers (2)

Richard
Richard

Reputation: 279

The most frequent problem with setting cookies (or any other header) is, that it has to be set before you send first output character. There may be an invisible character before your script (like BOM or whitespace). If you use includes, thare may be a whitespace after the closing tag of your included script.

Upvotes: 3

Your Common Sense
Your Common Sense

Reputation: 158004

To debug cookies, one have to debug HTTP headers.
So, get yourself a Firebug, switch to Net tab and watch request and response headers, if you can see any cookie sent by server or returned by browser.

If there will be no trace of cookies - there is some error on the server side then. You need to enable error reporting and displaying in order to see them

ini_set('display_errors',1);
error_reporting(E_ALL);

is a quick and dirty way

Upvotes: 0

Related Questions