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.

3 responses to “Cookies with PHP’s setcookie() and Safari”

  1. Henrik says:

    I noticed something strange in Iphone. I´m still able to set and retrieve cookies on Iphone Safari, even after going to Safari Settings and disabling cookies (set to never accept cookies). Anyone else experienced this ?

  2. rulo says:

    This works fine, but is useless when the path is just a slash ‘/’

  3. Sam says:

    so what can be done with a slash as a path??
    Seems it’s impossible to delete cookies in safari, using the root path(