当我更改页面时,cookie消失了 [英] the cookie disappear when I change page
问题描述
(PHP)我以这种方式在我的login.php页面中设置了cookie:
(PHP) I set the cookie in my login.php page in this way:
setcookie('cookie_id',$id);
我打印cookie并看到正确的值,但是当我用以下方法更改页面时:
I print the cookie and I see the correct value but when I change page with:
header($login_url);
我丢掉了所有cookie,不知道为什么。有人可以帮助我吗?
I lose the all cookie and I don't know why. Anybody can help me?
推荐答案
您必须指定 /
作为路径在 setcookie()
函数中,因此cookie在您网站的每个路径上都可用。为此:
You have to specify /
as path in setcookie()
function, so cookie will be available on every path of your site. To do this:
setcookie('cookie_id', $id, 0, '/');
请注意,第三个参数是到期时间,默认设置为0。根据文档,它表示:
Note that third argument is expire time which is set to 0 as default. According to documentation it means that:
如果设置为0或省略,则cookie将在会话结束时(浏览器关闭时)过期。
If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).
这篇关于当我更改页面时,cookie消失了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!