We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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();
}


59.9k

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



59.9k

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.phalcon.io/en/latest/reference/cookies.html



59.9k

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.