[web] deleting a cookie

Started by
1 comment, last by markr 17 years, 3 months ago
Hello, Is there a simple way to delete a cookie created by setcookie()? Thanks, exorcist_bob
Advertisement
According to this you can set the value to false and it will be removed.
That isn't true. Cookies have a string value (not boolean). FALSE will simply be converted to an empty string and the cookie set to an empty string, which is different from removing it.

To remove a cookie, simply set it with the same name, domain, path and security as you originally set it, but with an expiration date in the past. Note that the value is irrelevant as it will be immediately thrown away (I used "Delete" here).

I use:

        $expiry = gmmktime(0,0,0, // hms                1, // month                1, // day                2005 // year                );        setcookie(AUTH_COOKIE_NAME, 'Delete',                 $expiry,  // expiry                '/'); // path                // optional: include domain                 // optional: include security


In this case AUTH_COOKIE_NAME is a constant with the name of my cookie.

If you set it with the wrong path, domain or security level, the cookie won't be deleted - they must all match exactly.

In practice, if you have full control over the code setting the cookie, deleting it should just be a matter of using the same parameters (except having it expire in the past).

Mark

This topic is closed to new replies.

Advertisement