cookies - PHP bypass 'pseudo asynchrony' of setcookie(); function -
here 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 following;
empty! :(
it first seems setcookie()
executed asynchronously, not if give little thought setcookie()
set cookie header. (little server<->browser talks)
the problem need access newly created cookie right away. how that?
is way come one:
if (!empty($_get['supahcookie'])) { setcookie("mycookie", $_get['supahcookie'], time()+3600*24*31); unset($_get['search_type']); // avoind redirect loop header('location: ./?'.http_build_query($_get)); }
well.. there one, bit messier one:
$foo = empty($_get['supahcookie'])?(empty($_cookie['mycookie'])?'empty! :(':$_cookie['mycookie']):$_get['supahcookie'];
am inventing wheel here on again?
does have other, more elegant solutions?
no. $_cookie
, rest of superglobals populated part of php's startup routines. after startup completed, php not touch them ever again (except $_session, khez points out below) duration of script's execution. set it+forget it, basically. none of php functions affect superglobals (particularly set_cookie()) ever touch arrays again, it's not "asynchronous". it's disassociated. time cookie shows in $_cookie when it's present in http headers of request caused script executed.
you can overload setcookie want, should not so. $_cookie represents state of client when made request. if start writing new data $_cookie, you're losing state of client.
Comments
Post a Comment