Deleting a Cookie

setcookie('version', '', time()  10*365*24*60*60);

An intuitive way to delete a cookie is to set its value to an empty string. However, the cookie is still there, albeit without a value. A better way is to additionally send a cookie with the same name again, but provide an expiry date that is in the past. Again, incorrect local time settings have to be taken into account, so use a really small expiry date, for instance 10 years before today. This listing implements this and deletes the version cookie that has been sent by the previous listings. Here, both methods are combined: The cookie value is set to an empty string, and the expiry date is in the past. Figure shows the result: The browser tries to delete the cookie by setting the expiry date to the desired time (which lies in the past, so the cookie is gone).

Deleting a Cookie
  setcookie('version', '', time()
Tried to delete cookie.

The cookie will be deleted (if the user accepts it).

If you try to set the expiration date to 0, PHP just skips this parameter, so this does not work. You do have to provide a positive parameter, even if it's 1.