Remove a cookie

后端 未结 22 1303
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 16:16

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

相关标签:
22条回答
  • 2020-11-22 16:34

    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);
    
    0 讨论(0)
  • 2020-11-22 16:34

    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 :)

    0 讨论(0)
  • 2020-11-22 16:36

    See the sample labelled "Example #2 setcookie() delete example" from the PHP docs. To clear a cookie from the browser, you need to tell the browser that the cookie has expired... the browser will then remove it. unset as you've used it just removes the 'hello' cookie from the COOKIE array.

    0 讨论(0)
  • 2020-11-22 16:37

    Just set the expiration date to one hour ago, if you want to "remove" the cookie, like this:

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

    or

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

    Source: http://www.php.net/manual/en/function.setcookie.php

    You should use the filter_input() function for all globals which a visitor can enter/manipulate, like this:

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

    You can read more about it here: http://www.php.net/manual/en/function.filter-input.php and here: http://www.w3schools.com/php/func_filter_input.asp

    0 讨论(0)
  • 2020-11-22 16:37

    Most of you are forgetting that this will only work on a local machine. On a domain you will need a pattern like this example.

    setcookie("example_cookie", 'password', time()-3600, "/", $_SERVER['SERVER_NAME']);
    
    0 讨论(0)
  • 2020-11-22 16:38

    Just set the value of cookie to false in order to unset it,

    setcookie('cookiename', false);
    

    PS:- That's the easiest way to do it.

    0 讨论(0)
提交回复
热议问题