PHP setcookie() 在取消设置 $_COOKIE 后不起作用

发布于 2024-10-10 06:41:11 字数 1336 浏览 0 评论 0原文

我刚刚为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不回头走下去 2024-10-17 06:41:11

我认为您误解了 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 your Cookie::set method.

If you are "testing" Cookie::set by looking at the contents of $_COOKIE (or by using Cookie::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 inside Cookie::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.

时光是把杀猪刀 2024-10-17 06:41:11

如果你想改变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 . call Session::remove('session_name'); only when you don't need the cookie anymore.

许仙没带伞 2024-10-17 06:41:11

如果我正确理解你的话,你需要这样的东西

public static function set($name, $value,$expire) 
{
    setcookie($name, $value, $expire);

   $_COOKIE[$name] = $value;    

}

if I correctly understood you , you need something like that

public static function set($name, $value,$expire) 
{
    setcookie($name, $value, $expire);

   $_COOKIE[$name] = $value;    

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文