Cookie not deleted - PHP

Asked

Viewed 356 times

0

I don’t know what mistake I’m making when creating a class, but I’m not able to delete the cookie with my class.

public function cookie($name, $value = 'nome', $expire = NULL, $path = '/', $domain = NULL, $secure = FALSE, $httponly = TRUE) {
                setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
                return $this;
        }

public function delete($name){

            unset($_COOKIE[$name]);
            // empty value and expiration one hour before
            setcookie($name, NULL, -1);
            //return var_dump($_COOKIE);
        }

$cs->cookie('meu', 'sd', time()+1800);
$cs->delete('meu');

I don’t know why else in my browser the cookie is not deleted, what I’m doing wrong?

1 answer

1


After testing, here’s the code working:

function cookie($name, $value = 'nome', $expire = NULL, $path = '/', $domain = NULL, $secure = FALSE, $httponly = TRUE) {
                setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
                //return $this;
        }

function delete($name, $value = 'nome', $expire = NULL, $path = '/', $domain = NULL, $secure = FALSE, $httponly = TRUE) {
            unset($_COOKIE[$name]);
            setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
        }

cookie('omeucookie', 'sd', time()+1800);
delete('omeucookie', 'sd', 1);

To delete the cookie you need to send it its value (sd) and the path (/) you set earlier.

  • Ricardo, I honestly don’t understand. The cookie keeps popping up in Chrome (even an extension) and Firefox, but when I try to find the name and see that it exists, it says it doesn’t exist. But the cookie keeps popping up. When I don’t use the class, it removes the cookie and is left with nothing. I wonder what’s wrong?

  • 1

    Okay, I figured it out! In addition to the cookie name you must also pass the value and the path: 
function delete($name, $value = 'nome', $expire = NULL, $path = '/', $domain = NULL, $secure = FALSE, $httponly = TRUE) {
 unset($_COOKIE[$name]);
 setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
 }
delete('omeucookie', 'sd', 1);


  • I ran some tests here, and in my case it’s path which is jamming everything, when I pass it, it removes the cookie. Hugs...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.