I'm trying to access a cookie's value (using $_COOKIE
) immediately after calling the setcookie()
function in PHP. When I do so, $_COOKIE['uname']
isn't set. Why?
Note, however, that $_COOKIE['uname']
is set as expected upon the next execution of the script, such as after a page refresh.
setcookie('uname', $uname, time() + 60 * 30);
echo "Cookie value: " . $_COOKIE['uname'];
$_COOKIE
is set when the page loads, due to the stateless nature of the web. If you want immediate access, you can set$_COOKIE['uname']
yourself or use an intermediate variable.For example: