I noticed that Message-ID: from
Bonge Boo! contained the following:
>Sorry to be a pain, but its the answer about cookies that I want. I know I
>can achieve the same things, but I have good reasons for wanting to do it
>this way. If I can't fine. But I need to know why the cookie is doing what
>it is doing, and if it a programming fault at my end or the correct
>"behaviour"
Yep it's correct and it can do your head in unless you get the sequence
of events correct.
Remember, cookies are small amounts of data stored on the client
machine( I'm assuming you are familiar with the terms client and
server). Consider what happens.
The first time you call a php script you send an HTTP request from the
client to the server (by clicking a button on a form, following a link
etc). This request contains the cookie information if it is set. If no
cookie is set so the script cannot receive one. The script may then
respond and set or update a cookie. Any value taken from the cookie
will obviousl;y be the one originally stored, not the updated one.
Say you make another HTTP request. This one contains the value of the
cookie and the script can read it and respond to it. So the script is
always behind. However, the cookie has been set, and can be read later.
Take your code:
setcookie('instruction_type', $_POST['instruction_type'], time()+1800);
print "We set the cookie to: " .$_COOKIE['instruction_type'];
So as you have seen it is working as predicted. If you did:
setcookie('instruction_type', $_POST['instruction_type'], time()+1800);
print "If your system allows it,
we have set a cookie with the value of: " .$_POST['instruction_type'];
It would at least read correctly, but you can not be certain the cookie
is set because the cookie is stored on the client and the only way the
server can know that is when it receives another request.
What you can do is this: The first time the page is served it should
attempt to initialise a cookie. When the form is posted, the script
should check that cookie is set. If it's not give a warning that the
session will not be saved. If it is, everything is fine.
HTH.
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs <a rel="nofollow" style='text-decoration: none;' href="http://www.ckdog.co.uk/rfdmaker/" target="_blank">http://www.ckdog.co.uk/rfdmaker/</a>
>> Stay informed about: Stupid setcookie() question...