Remove a cookie

后端 未结 22 1255
没有蜡笔的小新
没有蜡笔的小新 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:27

    When you enter 0 for time, you mean "now" (+0s from now is actually now) for the browser and it deletes the cookie.

    setcookie("key", NULL, 0, "/");
    

    I checked it in chrome browser that gives me:

    Name: key
    Content: Deleted
    Created: Sunday, November 18, 2018 at 2:33:14 PM
    Expires: Sunday, November 18, 2018 at 2:33:14 PM
    
    0 讨论(0)
  • 2020-11-22 16:28

    If you want to delete the cookie completely from all your current domain then the following code will definitely help you.

    unset($_COOKIE['hello']);
    setcookie("hello", "", time() - 300,"/");
    

    This code will delete the cookie variable completely from all your domain i.e; " / " - it denotes that cookie variable's value all set for all domain not just for current domain or path. time() - 300 denotes that it sets to a previous time so it will expire.

    Thats how it's perfectly deleted.

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

    A clean way to delete a cookie is to clear both of $_COOKIE value and browser cookie file :

    if (isset($_COOKIE['key'])) {
        unset($_COOKIE['key']);
        setcookie('key', '', time() - 3600, '/'); // empty value and old timestamp
    }
    
    0 讨论(0)
  • 2020-11-22 16:32

    It's simple!

    setcookie("cookiename", "cookievalue", 1);
    
    0 讨论(0)
  • 2020-11-22 16:33

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

    Set the value to "" and the expiry date to yesterday (or any date in the past)

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

    Then the cookie will expire the next time the page loads.

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