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 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).


WARNING

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.





PHP Phrasebook
PHP Phrasebook
ISBN: 0672328178
EAN: 2147483647
Year: 2005
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net