Skip to Content
PHP Cookbook
book

PHP Cookbook

by David Sklar, Adam Trachtenberg
November 2002
Intermediate to advanced
640 pages
16h 33m
English
O'Reilly Media, Inc.
Content preview from PHP Cookbook

8.3. Deleting Cookies

Problem

You want to delete a cookie so a browser doesn’t send it back to the server.

Solution

Call setcookie( ) with no value for the cookie and an expiration time in the past:

setcookie('flavor','',time()-86400);

Discussion

It’s a good idea to make the expiration time a few hours or an entire day in the past, in case your server and the user’s computer have unsynchronized clocks. For example, if your server thinks it’s 3:06 P.M. and a user’s computer thinks it’s 3:02 P.M., a cookie with an expiration time of 3:05 P.M. isn’t deleted by that user’s computer even though the time is in the past for the server.

The call to setcookie( ) that deletes a cookie has to have the same arguments (except for value and time) that the call to setcookie( ) that set the cookie did, so include the path, domain, and secure flag if necessary.

See Also

Recipe 8.2 shows how to set cookies; Recipe 8.3 shows how to read cookie values; Recipe 8.13 explains output buffering; Recipe 8.19 shows how to avoid the “headers already sent” error message that sometimes occurs when calling setcookie( ); documentation on setcookie( ) at http://www.php.net/setcookie.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
PHP Cookbook, 2nd Edition

PHP Cookbook, 2nd Edition

Adam Trachtenberg, David Sklar
PHP Cookbook, 3rd Edition

PHP Cookbook, 3rd Edition

David Sklar, Adam Trachtenberg
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe

Publisher Resources

ISBN: 1565926811Supplemental ContentCatalog PageErrata