PHP:浏览器中的Cookie,但接收到索引未定义的错误 [英] PHP: Cookie in browser, but receive index undefined error

查看:609
本文介绍了PHP:浏览器中的Cookie,但接收到索引未定义的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的代码中正确设置了我的Cookie,例如:

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

但是,当我尝试访问cookie以使用它的值( $如果我检查我的浏览器(如果你的用户名是userid = $ _COOKIE ['userid']; ),我一直收到,未定义的索引:userid



<在这种情况下,Firefox)cookie我可以清楚地看到我的cookie是存在和设置:





任何和所有的帮助是赞赏...这是推动我insance。谢谢!

解决方案

我看到cookie的'path'属性设置为服务器上的某个目录。路径限制范围。当路径未明确指定时,浏览器使用脚本uri。很可能你的脚本读取cookie是在不同的位置,那么设置cookie。尝试设置路径到/ eg

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


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

这篇关于PHP:浏览器中的Cookie,但接收到索引未定义的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆