If that's exactly what you have, you have a whitespace before the <?. Cookies are sent via header requests, so if you have output already its impossible to send the headers.
Open your error reporting:
This response doesn't really clarify anything since you don't mention whether the first line is the <? or if you are referring to setcookie being the first instruction.
Let me show you what I'm talking about:
Your code as it is shows a whitespace on line 1. That is output. Its already been mentioned as well that $_COOKIE does not get populated until the next request is performed.
One thing not yet mentioned is your use of short tags. Since this is a directive, you should not be using short_open_tags in your code. If it is not enabled, <? . . . ?> is considered as an element in HTML and will be rendered as such.
Finally, the cookie itself doesn't specify a path nor domain. This code all assumes that you are within the same directory. It is also assumed that you are directly attaching to this script and not including it.
One thing not yet mentioned is your use of short tags. Since this is a directive, you should not be using short_open_tags in your code. If it is not enabled, <? . . . ?> is considered as an element in HTML and will be rendered as such.
Very nice explanation on the whole cookie deal Fou-Lu, but in particular on the short tags which is one reason why every time i see a short tag i replace it with a full tag. The only time i use anything like a short tag is when im displaying something like
Code:
<?=$value;?>
other than that its all full tags. For some reason i have a problem or i just dont like php echo ???? i even try to stay away from php print, i dont know why im a fussy guss i guess lol.
Awhile back i had a tough time with cookie as well, fought it for days till Fou-Lou helped me out, same issue. In that case i pulled the value of the cookie to use on another value when the site loaded.
What i had to do was when the site first loaded i had to hard code a value for the var. Then after that it was fine, because the page was reloaded by the visitor after that and the cookie was set. But at first load of the site if you need that cookie value it is not there yet. Thats just the way it is.
You might have to use a session value for first load and then the cookie after that.
This response doesn't really clarify anything since you don't mention whether the first line is the <? or if you are referring to setcookie being the first instruction.
Let me show you what I'm talking about:
Your code as it is shows a whitespace on line 1. That is output. Its already been mentioned as well that $_COOKIE does not get populated until the next request is performed.
One thing not yet mentioned is your use of short tags. Since this is a directive, you should not be using short_open_tags in your code. If it is not enabled, <? . . . ?> is considered as an element in HTML and will be rendered as such.
Finally, the cookie itself doesn't specify a path nor domain. This code all assumes that you are within the same directory. It is also assumed that you are directly attaching to this script and not including it.
Read that, then you will start to understand what Fou is getting at. As you will see, you can set a cookie but until the client sends a new http request, the cookie will not be sent back to the server. If it isn't sent to the server then the $_COOKIE array will not contain your cookie. The $_COOKIE array only contains cookies that have been sent from the client to your server and script - not cookies that you set with setcookie().
Whenever you output *anything* it causes the reply headers to be sent. If you have whitespace as Fou has shown, that will also send the reply headers. Once those headers have been sent, you can not send a cookie or any other header. This is because the headers can only be sent once - otherwise you would have mixed html and headers and the browser would not know what is what. There is another explanation of this in my signature - see the link about headers already sent.
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.