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

其他回答

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

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

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

请参阅PHP文档中标记为“示例#2 setcookie() delete Example”的示例。要清除浏览器中的cookie,您需要告诉浏览器该cookie已经过期……然后浏览器将删除它。unset只是从cookie数组中移除'hello' cookie。

您可以根据cookie值设置会话变量

session_start();

if(isset($_COOKIE['loggedin']) && ($_COOKIE['loggedin'] == "true") ){
$_SESSION['loggedin'] = "true";
}

echo ($_SESSION['loggedin'] == "true" ? "You are logged in" : "Please Login to continue");

你可以试试这个

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

很简单!

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