I l @ ve RuBoard |
The final thing to understand about using cookies is how to delete one. While a cookie will automatically expire when the user's browser is closed or when the expiration date/time is met, sometimes you'll want to manually delete the cookie as well. For example, Web sites that have registered users and login capabilities will probably want to delete any cookies when the user logs out. Although the setcookie() function can take up to six arguments, only one is actually requiredthe name. If you send a cookie that consists of a name without a value it will have the same effect as deleting the existing cookie of the same name . For example, to create the cookie UserName, you use this line: setcookie("UserName", "Larry"); To delete the UserName cookie, you would code: setcookie("UserName", ""); As an added precaution, you can also set an expiration date that's in the past. setcookie("UserName", "", time() - 60); To demonstrate this feature, let's add a reset button to the cookies.php page that will destroy the sent cookies and display the default colors once again. Script 12.3. To reset all the values, blank cookies are sent with the same names as the existing cookies. A reset checkbox has also been added to the HTML form.
To delete a cookie:
Tip To test that your setcookie() functions are working properly, be sure to check "Warn me before accepting a cookie" (in Netscape) or "Ask for each cookie" (in Internet Explorer) in your browser's preferences (Figure 12.1). Tip The setcookie() function is one of the few in PHP that could generate different results in different browsers, since browsers will treat cookies differently. Be sure to test your Web sites in multiple browsers on different platforms to insure consistency. |
I l @ ve RuBoard |