Re: Cookie Trouble: getting the information back out...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Mar 25, 2008 at 9:31 PM, Daniel Brown <parasane@xxxxxxxxx> wrote:
> On Tue, Mar 25, 2008 at 9:11 PM, Mark Weaver <mdw1982@xxxxxxxxxxx> wrote:
>  > Hi all,
>  [snip!]
>
> >
>  >  Cookie Test Page
>  >  ==========================
>  >  if (isset($_COOKIE["cookiename"])){
>  >         list($first,$second,$third) = explode('|',$_COOKIE["cookiename"]);
>  >         echo "<p>I found your cookie</p>\n";
>  >         echo "<p>The following Values were Contained in the cookie:<BR>
>  >               Username: $first<BR>
>  >               Password: $second<BR>
>  >               Type    : $third</p>\n";
>  >  }
>  >  else{
>  >         echo "<p>I wasn't able to find your cookie.</p>\n";
>  >  }
>  >
>  >  Now, I've constructed a cookie_test.php page to check things out and the
>  >  strange behavior I'm seeing is, upon first execution I get the "else"
>  >  block, but if I hit the browser's reload button I get the "if" block. At
>  >  first I thought the cookie wasn't being read at all because of weird
>  >  characters, but then upon reloading the page and seeing the "if" block
>  >  being displayed I'm thoroughly confused. It's gotta something simple I'm
>  >  missing.
>
>     Is this block of code executed immediately after the cookie is
>  set?  Sometimes PHP works too fast for its own good and the client
>  doesn't even realize it has a cookie yet.  Try setting it with one
>  page and either sleep()'ing for a bit or forcing a link-click or page
>  refresh before checking for the cookie.
>

Um... Cookie data ISN'T available to the same script that sets it. If
you use setcookie(), all it does is send a header to the browser
immediately ahead of the output of your script telling the browser to
store those values in either memory or on disk. The value will not
appear in the $_COOKIE array until the browser requests the next page
and includes the Cookie: header as part of the request.

The part of the manual that applies is this line:

"Once the cookies have been set, they can be accessed on the next page
load with the $_COOKIE or $HTTP_COOKIE_VARS arrays."

$_SESSION variables are available immediately as soon as you set them.
The session cookie still isn't set on the client until the browser
processes the response headers at the end of the transaction, but the
values are already in the array and, if the session cookie works they
will be accessible on successive requests.

Andrew

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux