Praveen Pachauri February 2016
asking about the cookie after logout
I'm trying to access a cookie's value (using
$_COOKIE) immediately after calling the
setcookie() function in PHP. When I do so,
$_COOKIE[$cookiename] isn't set after login. Why?
$cookie_value = "John Doe";
setcookie($cookiename,$cookie_value, time() + (86400 * 30),"/");
echo $cookiename ;
codeHeart February 2016
This is because when you call
setcookie() you are attaching the cookie to the current response(when the current request completes).
$_COOKIE will have a value when a request is made with the cookie header already set i.e cookie is sent to the server.
So, when only #1 happens you don't have anything in $_COOKIE, but once #2 happens you will have a value in $_COOKIE
EDIT: Almost everything you need is mentioned in the manual
setcookie() defines a cookie to be sent along with the rest of the
HTTP headers Once the cookies have been set, they can be accessed on
the next page load with the $_COOKIE array. Cookie values may also
exist in $_REQUEST.
Asked in February 2016
Viewed 3,609 times Voted 8 Answered 1 times
Leave an answer
Quote of the day: live life
Devs Planet © all rights reserved