简体   繁体   中英

PHP: Cookie in browser, but receive index undefined error

I'm setting my cookie properly in my code, for example:

$expire = time()+60*60*24*30;          
setcookie("userid", 27, $expire);

But then when I attempt to access the cookie to use it's value ( $userid = $_COOKIE['userid']; ), I keep receiving, "undefined index: userid"

If I check my browser's (in this case, Firefox) cookies I can clearly see that my cookie is there and set:

在此处输入图片说明

Any and all assistance is appreciated...this is driving me insance. Thanks!

I see that cookie has 'path' attribute set to some directory on the server. Path limits the scope. When path is not explicitly specified browser uses script uri. Most likely your script which reads the cookie is at different location then the one which sets cookie. Try setting path to / eg

$expire = time()+60*60*24*30;          
setcookie("userid", 27, $expire,'/');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM