here is a code example:
if (!empty($_GET['supahCookie']))
{
setcookie("myCookie", $_GET['supahCookie'], time()+3600*24*31);
}
$foo = empty($_COOKIE['myCookie'])?'Empty! :(':$_COOKIE['myCookie'];
echo $foo;
The output will be the following;
Empty! :(
It first it seems like setcookie()
was executed asynchronously, but its not if you will give a little thought setcookie()
just set a cookie header. (little server<->browser talks)
The problem is that i need to access newly created cookie right away. How would i do that?
Is the only way i come up with is this one:
if (!empty($_GET['supahCookie']))
{
setcookie("myCookie", $_GET['supahCookie'], time()+3600*24*31);
unset($_GET['search_type']); // to avoind redirect loop
header('Location: ./?'.http_build_query($_GET));
}
Well.. there is another one, a bit messier one:
$foo = empty($_GET['supahCookie'])?(empty($_COOKIE['myCookie'])?'Empty! :(':$_COOKIE['myCookie']):$_GET['supahCookie'];
Am i inventing a wheel here all over again?
Does anyone have any other, more elegant solutions?