2024-02-28 09:00:02

移除一块饼干

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

unset($_COOKIE['hello']);

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


当前回答

你可以试试这个

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

其他回答

$cookie_name = "my cookie";
$cookie_value = "my value";
$cookie_new_value = "my new value";

// Create a cookie,
setcookie($cookie_name, $cookie_value , time() + (86400 * 30), "/"); //86400 = 24 hours in seconds

// Get value in a cookie,
$cookie_value = $_COOKIE[$cookie_name];

// Update a cookie,
setcookie($cookie_name, $cookie_new_value , time() + (86400 * 30), "/");

// Delete a cookie,
setcookie($cookie_name, '' , time() - 3600, "/"); //  time() - 3600 means, set the cookie expiration date to the past hour.

只要将过期日期设置为一小时前,如果你想“删除”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("example_cookie", 'password', time()-3600, "/", $_SERVER['SERVER_NAME']);

你必须删除cookie与php在你的服务器,也与js为您的浏览器..(他们是用php做的,但cookie文件也在浏览器客户端中):

一个例子:

if ($_GET['action'] == 'exit'){
            // delete cookies with js and then in server with php:
            echo '
            <script type="text/javascript">
                var delete_cookie = function(name) {
                     document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:01 GMT;";
                };
                delete_cookie("madw");
                delete_cookie("usdw");
            </script>
            ';
unset($_COOKIE['cookie_name']);
unset($_COOKIE['cookie_time']);

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

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