Home >Backend Development >PHP Tutorial >Why Can't I Access Cookies Immediately After Using `setcookie()`?
Why can't I access $_COOKIE after calling setcookie()?
Upon invoking the setcookie() function, the cookie itself is not immediately available in PHP's $_COOKIE array. This is because the cookie is not set until the HTTP response is sent back to the client, which happens after PHP scripts have completed executing.
Making Cookie Values Accessible
To circumvent this issue, you can manually add the cookie value to the $_COOKIE array after setting it with setcookie(). This way, you can access the value within the current script:
setcookie('uname', $uname, time() + 60 * 30); $_COOKIE['uname'] = $uname;
The above is the detailed content of Why Can't I Access Cookies Immediately After Using `setcookie()`?. For more information, please follow other related articles on the PHP Chinese website!