Skip to content
Advertisement

Can’t delete php set cookie

I’ve set a cookie through this call in php

setcookie('alert_msg', 'you have the add badge');

I have tried unsetting it this way

setcookie('alert_msg', '');
setcookie('alert_msg', false);
setcookie('alert_msg', false, 1);
setcookie('alert_msg', false, time()-3600);
setcookie('alert_msg', '', 1, '/');

and it still won’t unset the cookie value in $_COOKIE[‘alert_msg’].

I have tried in both Firefox and Chrome

Code sample:

if (isset($_COOKIE['alert_msg'])) {
    $this->set('alert_msg', $_COOKIE['alert_msg']);
    unset($_COOKIE['alert_msg']);
    setcookie('alert_msg', '', 1, '/');
}

Advertisement

Answer

Checkout the cookie path.

Since you are not passing the path parameter to the setcookie function, in this case the cookie will be set for the current directory only and can be used and can be unset from that directory only.

Possible solution is to pass the path value as /. So that cookie can be used and unset from any part of application.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement