2024-02-28 09:00:02

移除一块饼干

当我想要删除Cookie时,我会尝试

unset($_COOKIE['hello']);

我在firefox的cookie浏览器中看到cookie仍然存在。我怎么才能真正去掉饼干呢?


当前回答

当您为时间输入0时,您的意思是“现在”(从现在开始+0实际上是现在),浏览器会删除cookie。

setcookie("key", NULL, 0, "/");

我在chrome浏览器中检查了它,给我:

Name: key
Content: Deleted
Created: Sunday, November 18, 2018 at 2:33:14 PM
Expires: Sunday, November 18, 2018 at 2:33:14 PM

其他回答

只要将过期日期设置为一小时前,如果你想“删除”cookie,就像这样:

setcookie ("TestCookie", "", time() - 3600);

or

setcookie ("TestCookie", "", time() - 3600, "/~rasmus/", "example.com", 1);

来源:http://www.php.net/manual/en/function.setcookie.php

你应该使用filter_input()函数来处理访问者可以输入/操作的所有全局变量,如下所示:

$visitors_ip = filter_input(INPUT_COOKIE, 'id');

你可以在这里阅读更多:http://www.php.net/manual/en/function.filter-input.php和这里:http://www.w3schools.com/php/func_filter_input.asp

当您为时间输入0时,您的意思是“现在”(从现在开始+0实际上是现在),浏览器会删除cookie。

setcookie("key", NULL, 0, "/");

我在chrome浏览器中检查了它,给我:

Name: key
Content: Deleted
Created: Sunday, November 18, 2018 at 2:33:14 PM
Expires: Sunday, November 18, 2018 at 2:33:14 PM

我在php中使用这个,它工作得很好。

function cookie_unset()
{
    setcookie("cookie_name", "", time() - 3600, '/');
    unset ($_COOKIE['cookie_name']);
    //done
}

将值设置为"",过期日期设置为昨天(或过去的任何日期)

setcookie("hello", "", time()-3600);

然后,cookie将在下次页面加载时过期。

你可以试试这个

if (isset($_COOKIE['remember_user'])) {
    unset($_COOKIE['remember_user']); 
    setcookie('remember_user', null, -1, '/'); 
    return true;
} else {
    return false;
}