Delete all cookies on logout

Hi,

i want to delete all cookies on logout, but it is not working, only when i put this "if" into login.

public function logoutAction() {
    if (!empty($_COOKIE)) {
        foreach ($_COOKIE as $name => $value) {
            setcookie($name, $value, time() -1);
        }
    }

    $this->auth->remove();

    return $this->response->redirect('index');
}

Rgds Stefan

public function logoutAction()
{
    $cookie = $this->cookies->get("the-cookie-key-to-delete-here");

    // Delete the cookie
    $cookie->delete();
}


19.6k

Hello Serghei,

thx for your reply, i know that, but i have dynamic cookies. Something like:

cookie1, cookie2, cookie3 .....

Rgds Stefan

edited Dec '16

Usually you should know all the key-value pairs that you store in the cookies. Just delete each cookie in a loop for keys. This can be done easier, for example by creating custom Cookie Manager, or using Phalcon Registry to store keys



19.6k

I did this now:

$cookiesArray = array();
        foreach ($_COOKIE as $key => $val) {
            $cookiesArray[] = $key;
        }

        if (!empty($_COOKIE)) {
            foreach ($_COOKIE as $name => $value) {
                if (in_array($name, $cookiesArray)){
                    continue;
            }
                setcookie($name, $value, time() -1);
            }
        }

I save all cookie names into an array $cookiesArray and then i check if this is in_array(), also not working in function logout(), only in login.

You have to use the Cookies Manager instead of setcookie / setrawcookie. Refer to the: https://docs.phalconphp.com/en/latest/reference/cookies.html



19.6k

That is really strange, it is also not working.

I put it now into the login, i can live with that :-) It is not influence my system, so what.

Thx for your help.

edited Dec '16

Stop using $_COOKIES please :( As well setcookie.