PHP Phrasebook
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 5.4 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 (deletecookie.php)
<?php setcookie('version', '', time() 10*365*24*60*60); ?> Tried to delete cookie.
Figure 5.4. The cookie will be deleted (if the user accepts it).
|
Категории