That's the nature of cookies, you can't use the cookie value in the same process as setting it. It's easier to think of it like this:
A user requests a page. The server sends a header code to set the cookie, then the rest of the page
User selects another page to view, or refreshes the page. At this time the browser will also send all cookie data
The server recieves the request for another page view, and also the cookie data. It parses the page, and uses the cookie data if need be
The resulting page is sent back to the user
The basic idea to remember is, to use a cookie value the browser has to send the cookie back to the server! Therefore by very function it can't be used in the same go as when you set it. In step one, the browser would have also sent any existing cookies, and the resulting page would have been dependant on these cookies too, depending on the script!