PHP: Cookie in browser, but receive index undefined error

走远了吗. 提交于 2020-01-14 05:55:06

问题


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!


回答1:


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,'/');


来源:https://stackoverflow.com/questions/7394256/php-cookie-in-browser-but-receive-index-undefined-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!