Deleting a Cookie

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.

graphics/12sc03.jpg

graphics/12sc03a.jpg

To delete a cookie:

  1. Open cookies.php in your text editor (Script 12.2).

  2. Add another conditional within the existing if ($BeenSubmitted) conditional (Script 12.3 lines 3-8).

     if ($Reset) {       setcookie("BGColor", "",         time()- "100");       setcookie ("TextColor", "" ,         time()-"100");       $BGColor = "WHITE";       $TextColor = "BLACK";    }else{ 

    If the form has been submitted, the PHP will first check to see if $Reset is TRUE. If it is, the script will delete the existing cookies by sending valueless cookies of the same name. The page will also reset the color values for the existing page.

  3. Complete the if ($Reset) conditional.

     setcookie("BGColor", "$NewBGColor",   time()+ "1000000");         setcookie ("TextColor",           "$NewTextColor", time()+           "1000000");       $BGColor = $NewBGColor;       $TextColor = $NewTextColor;    } 

    If the form has been submitted but $Reset is not TRUE, the form should be handled as it had been in Script 12.2.

  4. Within the HTML form, add a checkbox for the user to reset the colors (Script 12.3, line 47).

     <P><INPUT TYPE=Checkbox NAME=Reset   VALUE=TRUE> Check this box to reset   the colors.<P> 

    This checkbox will tell the PHP whether or not to reset the values. I have added two paragraph tags to make the HTML form look nicer.

  5. Save the script, load it to the server, and test it in your Web browser (Figures 12.11, 12.12 and 12.13).

    Figure 12.11. A simple checkbox has been added to the HTML form giving the user the option of resetting the page. It will also delete the existing cookies.

    graphics/12fig11.jpg

    Figure 12.12. When the setcookie() function is used with a name but no value, the existing cookie of that name will be deleted. The expiration date in the past also guarantees proper destruction of the existing cookie.

    graphics/12fig12.jpg

    Figure 12.13. If the user has checked the reset box and clicked Submit!, the PHP will destroy the cookies (Figure 12.12) and reset the form to its default colors.

    graphics/12fig13.gif

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


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

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