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

From: Date: Wed, 26 Mar 2008 01:31:57 +0000
Subject: Re: Cookie Trouble: getting the information back out...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-272095@lists.php.net to get a copy of this message
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. 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. Conversely, $_SESSION data is much quicker, since the PHPSESSID cookie is sent as soon as you initialize the session (session_start()), and you can then immediately access the variables. Proof-of-concept: <?php // session-test.php session_start(); $_SESSION['test'] = "This is only a test."; echo $_SESSION['test']."<br />\n"; ?> <?php // cookie-test.php setcookie("cookiename","This is a cookie test.",time()+86400); echo $_COOKIE['cookiename']."<br />\n"; ?> -- </Daniel P. Brown> Forensic Services, Senior Unix Engineer 1+ (570-) 362-0283

« previous php.general (#272095) next »