1 - copy specific GET vars into COOKIE (using header() )
2 - redirect user to URL with no GET string
3 - copy values out of COOKIEs for use by script
4 - destroy COOKIES
I have this code:
Code: Select all
$keepers = array('x', 'y', 'z'); // a list of specific GET variables I want to preserve
foreach($keepers as $k)
{
if(isset($_COOKIE[$k])) // this variable exists as a cookie
{
$kept[$k] = $_COOKIE[$k]; // copy value into a script var
header("Set-Cookie: {$k}=; Discard"); // destroy the cookie
}
}
var_dump($kept);
die();$kept contains a named key for x, but the value is blank. If I comment out the cookie-destroying header, $kept has a value for x. I'm getting the behavior I would expect if I were creating a reference to $_COOKIE['x'] instead of passing its value.
I also tried modifying the code, like so:
Code: Select all
... if(isset($_COOKIE[$k]))
{
$o .= $k . '=' . $_COOKIE[$k] . '<br />'; // check the value before destroying the cookie
header("Set-Cookie: {$k}=; Discard"); // destroy the cookie
$o .= $k . '=' . $_COOKIE[$k] . '<br />'; // check the value after destroying the cookie
}
print($o);The way I understand things, the first line should have a value for the cookie, and the second should not. Instead, it's like statement order doesn't matter, or (as I mentioned earlier) the variable is being passed by reference. I have even tried writing the value into an output buffer and grabbing the value from that, but I get the same results: clearing the cookie anywhere in the script prevents any code from seeing the value, even before it is cleared.
If anyone has an explanation why this is the case, I'd love to hear it.