How to remove a cookie with PHP?

Spread the love

Sometimes, we want to remove a cookie with PHP.

In this article, we’ll look at how to remove a cookie with PHP.

How to remove a cookie with PHP?

To remove a cookie with PHP, we can use the unset and setcookie functions.

For instance, we write

if (isset($_COOKIE["remember_user"])) {
    unset($_COOKIE["remember_user"]);
    setcookie("remember_user", null, -1, "/");
    return true;
} else {
    return false;
}

in a function.

We check if the cookie with name 'remember_user' is set with isset.

If it is, then we call unset to remove it from the $_COOKIE array.

And we call setcookie to send the 'remember_user' cookie with -1 to make it expire.

Conclusion

To remove a cookie with PHP, we can use the unset and setcookie functions.

Leave a Reply

Your email address will not be published.