Originally Posted by Fou-Lu
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
3. setcookie('email',"testtest",time() + (86400 * 365));
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.
Just to add to Fous cookie explanation..
If you don't understand what request and reply headers are, how cookies are sent and recieved etc, I have a post here which will explain this:
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.