Cookies with PHP’s setcookie() and Safari

I’ve come across a little inconsistency between browsers and the way they handle cookies. Using the examples from PHP’s online manual, I was unable to get Safari to remove cookies using setcookie(). All other browsers would clear the cookie with no problems. It turns out that Safari will only delete cookies if both the cookie name AND the cookie path match.

In other words, this won’t work:

setcookie('TestCookie', 'somevalue', time()+3600, '/somepath/'); // Set cookie
setcookie('TestCookie', '', time()-3600); // Delete cookie

But this will work:

setcookie('TestCookie', 'somevalue', time()+3600, '/somepath/'); // Set cookie
setcookie('TestCookie', '', time()-3600, '/somepath/'); // Delete cookie

Basically, if you set a cookie using a path, you must also specify the path when deleting the cookie. Safari won’t remove the cookie otherwise. This is probably a good thing.