problem in cookies

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

nate
New php-forum User
New php-forum User
Posts: 3
Joined: Tue Aug 20, 2002 10:41 am
Contact:

problem in cookies

Postby nate » Tue Aug 20, 2002 10:49 am

I having the problem with cookies. The cookies will be only created when I refresh the page twice. During the first time the page is being executed, the result of creating a cookie is success but i can't get any value from the cookie i created. Then I manage to get the value when i refresh the page. Is there any problem regarding to the apache or php option. Besides, I never specify any directory for this cookie but the result is I can only get cookie from the directory it has been created. Is there any solution for this?

the code for creating cookies is as below:
setcookie("cookie_1", "test1");
setcookie("cookie_2", "test2");

Jay

Postby Jay » Tue Aug 20, 2002 1:38 pm

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:

Step 1:
A user requests a page. The server sends a header code to set the cookie, then the rest of the page

Step 2:
User selects another page to view, or refreshes the page. At this time the browser will also send all cookie data

Step 3:
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

Step 4:
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!


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest