Remove a cookie

2018-12-31 16:49发布

When I want to remove a Cookie I try

unset($_COOKIE['hello']);

I see in my cookie browser from firefox that the cookie still exists. How can I really remove the cookie?

标签: php cookies
21条回答
浮光初槿花落
2楼-- · 2018-12-31 17:05

To reliably delete a cookie it's not enough to set it to expire anytime in the past, as computed by your PHP server. This is because client computers can and often do have times which differ from that of your server.

The best practice is to overwrite the current cookie with a blank cookie which expires one second in the future after the epoch (1 January 1970 00:00:00 UTC), as so:

setcookie("hello", "", 1);
查看更多
浮光初槿花落
3楼-- · 2018-12-31 17:07

You have to delete cookies with php in your server and also with js for your browser.. (They has made with php, but cookie files are in the browser client too):

An example:

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']);
查看更多
几人难应
4楼-- · 2018-12-31 17:08

That will unset the cookie in your code, but since the $_COOKIE variable is refreshed on each request, it'll just come back on the next page request.

To actually get rid of the cookie, set the expiration date in the past:

// set the expiration date to one hour ago
setcookie("hello", "", time()-3600);
查看更多
忆尘夕之涩
5楼-- · 2018-12-31 17:08

If you set the cookie to expire in the past, the browser will remove it. See setcookie() delete example at php.net

查看更多
只靠听说
6楼-- · 2018-12-31 17:10

You May Try this

if (isset($_COOKIE['remember_user'])) {
    unset($_COOKIE['Hello']);
    unset($_COOKIE['HelloTest1']);
    setcookie('Hello', null, -1, '/');
    setcookie('HelloTest1', null, -1, '/');
    return true;
} else {
    return false;
}
查看更多
柔情千种
7楼-- · 2018-12-31 17:10

I know that there has been a long time since this topic has been created but I saw a little mistake within this solution (I can call it like that, because it's a detail). I agree that the better solution is probably this solution:

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

But, in the present case, you delete the cookies in every case where the unset function works and immediately you create new expired cookies in case that the unset function doesn't work.

That means that even if the unset function works, it will still have 2 cookies on the computer. The asked goal, in a logical point of view, is to delete the cookies if it is possible and if it really isn't, make it expire; to get "the cleanest" result.

So, I think we better should do:

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

Thanks and have a nice day :)

查看更多
登录 后发表回答