【问题标题】:php can't delete cookiephp无法删除cookie
【发布时间】:2013-01-16 15:22:46
【问题描述】:

我有这个功能来设置一个cookie:

private function setCookie($key, $value){
        if(setcookie(
            $key,
            $this->encrypt($value),
            time() + 2592000,
            adminpath,
            database::getDomain(),
            database::getHTTPS(),
            true
        )){ // set cookie for a month
            return true;
        }
        else{ // cookie could not be created, write to errorlog
             $error  = array(
                "type"      => "other",
                "argument"  => 1,
                "class"     => __CLASS__,
                "function"  => __FUNCTION__,
                "errorMsg"  => "Could not create cookie ".$key." with value ".$value,
                "file"      => __FILE__,
                "line"      => __LINE__
            );
            $this->errorlog->log($error);
            return false;
        }
    }

然后我使用此代码取消设置 cookie:

private function destroyCookie($key){
        if(setcookie(
            $key,
            " ",
            time() - (time() + 2592000), 
            adminpath,
            database::getDomain(),
            database::getHTTPS(),
            true
        )){
            return true;
        }
        else{
            $error  = array(
                "type"      => "other",
                "argument"  => 1,
                "class"     => __CLASS__,
                "function"  => __FUNCTION__,
                "errorMsg"  => "Could not destroy cookie ".$key,
                "file"      => __FILE__,
                "line"      => __LINE__
            );
            $this->errorlog->log($error);
            return false;
        }
    }

我可能遗漏了一些非常简单的东西,但我不知道为什么我的 cookie 没有被删除。 这两个函数在同一个类中,函数 database::getDomain() 生成“www.creetab.com”,函数 database::getHTTPS() 生成“false”。管理路径是“/admin/”。 有人可以帮我解决这个问题吗? 设置cookie效果很好,只是删除了不起作用的cookie。

【问题讨论】:

    标签: php session cookies session-cookies


    【解决方案1】:
    $key,
    " ",
    time() - (time() + 2592000), 
    

    应该是:

    $key,
    "",
    time() - 2592000, 
    

    否则你只是给它一个空白空间的值,它仍然是一个值。你想通过让它为空来让它没有任何价值。同样设置过期时间。

    【讨论】:

      猜你喜欢
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 2012-01-20
      • 2012-07-06
      • 2019-11-13
      相关资源
      最近更新 更多