Re: Cookie Trouble: getting the information back out...
| From: | Mark Weaver | Date: | Wed, 26 Mar 2008 01:59:39 +0000 |
| Subject: | Re: Cookie Trouble: getting the information back out... | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-272100@lists.php.net to get a copy of this message | ||
Andrew Ballard wrote:
On Tue, Mar 25, 2008 at 9:31 PM, Daniel Brown <parasane@gmail.com> wrote:Thank you Andrew... Now it all makes perfect sense. Good grief! there's so much to learn. It seems that Java was easier. ;) -- Mark ------------------------- the rule of law is good, however the rule of tyrants just plain sucks! Real Tax Reform begins with getting rid of the IRS. ============================================== Powered by CentOS5 (RHEL5)On Tue, Mar 25, 2008 at 9:11 PM, Mark Weaver <mdw1982@mdw1982.com> 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.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. AndrewIs this block of code executed immediately after the cookie isset? 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.