setcookie to an empty value not working

前端 未结 5 2063
天涯浪人
天涯浪人 2020-12-21 06:46

i have this code im trying to do for a type of cache system so it remembers the city the user has selected. if the user has selected a city it stores it in sessions and cook

相关标签:
5条回答
  • 2020-12-21 07:25

    You can't set a cookie to most falsy values to indicate falseness of a trit cookie. Only '0' will work. Use that.

    0 讨论(0)
  • 2020-12-21 07:30

    Make sure to set your cookie with a negative time:

    setcookie($variable, '', -1);
    
    0 讨论(0)
  • 2020-12-21 07:37

    You can set empty value to the cookie by using null pointer as the value

    like this:

        setrawcookie('testEmptyCookie', "\x00", time() + 3600, '/');
    

    (tried on php 5.6, 7.2)

    0 讨论(0)
  • 2020-12-21 07:40

    PHP's setcookie() doesn't allow you to set cookies with empty values. But you can do that with header()

    replace:

    setcookie($variable, $value, time() + 31536000);
    

    with:

    header('set-cookie: '.rawurlencode($variable).'='.rawurlencode($value).'; max-age=31536000', false);
    
    0 讨论(0)
  • 2020-12-21 07:43

    You can't set a cookie with an empty string as it will delete the cookie.

    From the docs:

    If the value argument is an empty string, or FALSE, and all other arguments match a previous call to setcookie, then the cookie with the specified name will be deleted from the remote client.

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