PHP setcookie() 在取消设置 $_COOKIE 后不起作用
我刚刚为 Cookie 编写了非常简单的包装类,如下所示:
<?php
class Cookie {
// expire time of the cookie 31 days
private static $_expire = '2678400';
public static function set($name = null, $value = null, $expire = null) {
if (!empty($name)) {
$expire = !empty($expire) ? $expire : time() + self::$_expire;
if (setcookie($name, $value, $expire)) {
return true;
}
return false;
}
return false;
}
public static function get($name = null) {
if (!empty($name)) {
return !empty($_COOKIE[$name]) ? $_COOKIE[$name] : false;
}
return false;
}
public static function remove($name = null) {
if (!empty($name)) {
if (!empty($_COOKIE[$name])) {
if (setcookie($name, false, time() - self::$_expire)) {
unset($_COOKIE[$name]);
return true;
}
return false;
}
return true;
}
return false;
}
}
?>
但是,在最初设置 cookie 时我遇到了问题,然后我想通过首先调用 : 来更改值,
Cookie::remove('session_name');
然后
Cookie::set('session_name');
第二个(设置)没有设置cookie。
知道可能是什么原因造成的吗?
I've just written very simple wrapper class for Cookies, which goes as follow:
<?php
class Cookie {
// expire time of the cookie 31 days
private static $_expire = '2678400';
public static function set($name = null, $value = null, $expire = null) {
if (!empty($name)) {
$expire = !empty($expire) ? $expire : time() + self::$_expire;
if (setcookie($name, $value, $expire)) {
return true;
}
return false;
}
return false;
}
public static function get($name = null) {
if (!empty($name)) {
return !empty($_COOKIE[$name]) ? $_COOKIE[$name] : false;
}
return false;
}
public static function remove($name = null) {
if (!empty($name)) {
if (!empty($_COOKIE[$name])) {
if (setcookie($name, false, time() - self::$_expire)) {
unset($_COOKIE[$name]);
return true;
}
return false;
}
return true;
}
return false;
}
}
?>
However I have a problem when the cookie was initially set, then I want to change the value by first calling :
Cookie::remove('session_name');
and then
Cookie::set('session_name');
The second one (set) doesn't set the cookie.
Any idea what might be causing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您误解了 cookie 的工作原理。
$_COOKIE
的内容在 HTTP 请求到达时且脚本开始执行之前设置一次。如果您使用
setcookie
添加或修改 cookie,则在向您的服务器发出下一个 HTTP 请求之前,此添加或修改将不可见。这就是您在 Cookie::set 方法中所做的事情。如果您通过查看
$_COOKIE
的内容(或使用Cookie::get
)来“测试”Cookie::set
,这会相同的事情)那么您将看不到 cookie 的更改,即使已进行。要查看您期望的结果,您应该将该值添加到
Cookie::set
内的$_COOKIE
中。但是,我建议以不同的方式编写程序。您试图像使用普通变量一样使用 cookie,但事实并非如此。I think you misunderstand how cookies work.
The contents of
$_COOKIE
are set once, when the HTTP request arrives and before your script starts execution.If you use
setcookie
to add or modify a cookie, this addition or modification will not be visible until the next HTTP request to your server. This is what you are doing in yourCookie::set
method.If you are "testing"
Cookie::set
by looking at the contents of$_COOKIE
(or by usingCookie::get
, which does the same thing) then you will not see the changes to the cookie even though they have been made.To see what you expect, you should add the value to
$_COOKIE
insideCookie::set
. However, I would suggest writing your program in a different manner. You are trying to use cookies like normal variables, which they are not.如果你想改变cookie的值,不需要先删除它,你可以直接调用
Session::set('session_name');
,cookie就会被覆盖。仅当您不再需要 cookie 时才调用Session::remove('session_name');
。if you whant to change the value of a cookie , there is no need to first remove it , you can call straight
Session::set('session_name');
and the cookie would be overwrited . callSession::remove('session_name');
only when you don't need the cookie anymore.如果我正确理解你的话,你需要这样的东西
if I correctly understood you , you need something like that