cookie 设置不是立即生效的, setcookie()
只是在 response header 头里加上 Set-Cookie
头,例如:
Set-Cookie: PHPSESSID=tbmh6ussjmqrblhth75qb9h2l0; path=/
这个响应到达客户端, Cookie 设置成功后, 下次 客户端的请求就会在 Header 里带上这个头:
Cookie: PHPSESSID=tbmh6ussjmqrblhth75qb9h2l0
此时才能通过 $_COOKIE
取到这个值。这就是为什么:
they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS
Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS
php对setcookie函数的说明。