There is a page, that can be visited with one of three params: ref1, ref2 and ref3. Each time page script sets a couple of cookies to store ref-param and time of it's creation. After that it should find the latest one and be ready to deal with it. For example if I have "ref1" cookie as latest before visiting page and visit
script.php?ref2=1
it should echo
latest is ref2
But it works properly (echoes right latest cookie) only after additional reloading of the page. What's wrong with this code?
if ($_GET['ref1']) {
setcookie('ref1', 'ref1');
setcookie('ref1_dt', time());
}
if ($_GET['ref2']) {
setcookie('ref2', 'ref2');
setcookie('ref2_dt', time());
}
if ($_GET['ref3']) {
setcookie('ref3', 'ref3');
setcookie('ref3_dt', time());
}
function getLatestCookie() {
$refs = array(
'ref1' => $_COOKIE['ref1_dt'],
'ref2' => $_COOKIE['ref2_dt'],
'ref3' => $_COOKIE['ref3_dt']
);
$maxs = array_keys($refs, max($refs));
return $maxs[0];
}
$latest = getLatestCookie();
setcookie('latest', $latest, time() + 60 * 60 * 24 * 30, "/");
echo "latest is " . $latest;