2024-02-28 09:00:02

移除一块饼干

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

unset($_COOKIE['hello']);

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


当前回答

只要将过期日期设置为一小时前,如果你想“删除”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

其他回答

只要将过期日期设置为一小时前,如果你想“删除”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

你可以试试这个

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

要删除所有cookie,您可以这样写:

foreach ($_COOKIE as $key => $value) {
    unset($value);
    setcookie($key, '', time() - 3600);
}

一个干净的删除cookie的方法是清除$_COOKIE值和浏览器cookie文件:

if (isset($_COOKIE['key'])) {
    unset($_COOKIE['key']);
    setcookie('key', '', time() - 3600, '/'); // empty value and old timestamp
}

很简单!

setcookie("cookiename", "cookievalue", 1);