in reply to Cookie flexibility and limitations
Yes you need to write the cookies in the header. Perhaps you should write the header after you know what you're going to put in it? You should be able to retrieve a cookie as many times in your script as you want.