Chapter 9 Exercises

Exercise 4: Deleting a Cookie

The final concept to understand about using cookies is how to delete one. Although a cookie automatically expires when the end user’s browser is closed or when the expiration date/time is met, sometimes you will want to manually delete the cookie as well. For example, websites that have registered users and login capabilities generally delete any cookies when the user logs out.

The setcookie() function can take up to six arguments, but only one is required—the 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:

The setcookie() function example in PHP.

To delete the username cookie, you code:

The setcookie() function that deletes the username cookie.

As an added precaution, you can also set an expiration date in the past:

The setcookie() function that deletes the username cookie using the expiration date.


In this exercise you will add a reset feature to the web application that will destroy the sent cookies and display the default colours once again.

Note: A number of Helpful Tips are located at the end of this web page for reference as you complete this exercise.

To delete cookies:

Script 9-4: This form is used to select the background and font colours for another PHP page.
The PHP script that selects the background and font colors for another PHP page as displayed in the editor view.

  1. To begin, create a new PHP document (Script 9-4) in Brackets.
    A new PHP document as displayed in the editor view.
  2. Delete the existing cookies by sending blank cookies:
    The PHP code snippet that deletes the existing cookies by sending blank cookies as displayed in the editor view.
    These two lines send cookies named bg_color and font_color, each with no value and an expiration time of one minute ago. As you did when creating cookies, you must call the setcookie() function before anything else is sent to the web browser.
  3. Complete the PHP code and create the HTML head with the title Reset Your Settings:
    The code snippet that completes the PHP code and creates the HTML head with the title Reset Your settings as displayed in the editor view.
  4. Print the page's body:
    The HTML code snippet that prints the page's body as displayed in the editor view.
    The body of this script merely tells the user that their settings have been reset. A link is then provided to return to the main page.
  5. Complete the HTML:
    HTML closing tag as displayed in the editor view.
  6. Save the file as reset.php and upload it to the production server at studentuploads.ca to the chapter9 directory (https://www.studentuploads.ca/firstl/202/chapter9), and test in your browser (Figures 9-14 and 9-15). Ensuring you are looking at the production server version of the file, and ensuring it is saved to the same location as customize.php and view_settings.php.
    Figure 9-13: Note that when you run this script, you will still be able to see the cookie as saved in your list of sites. That is because, when the setcookie() function is used with a name but no value, the existing cookie of that name is deleted. The expiration date in the past also guarantees proper destruction of the existing cookie.
    The setcookie() function alert as displayed in the browser view.
    Figure 9-14: The reset page sends two blank cookies and then displays this message.
    The reset page that sends two blank cookies and then displays this message as displayed in the browser view.
    Figure 9-15: With the reset page, PHP destroys the cookies (Figure 9-13), which resets the main page to its default colours.
    The page that can reset or customize the cookie and set as displayed in the browser view.
Important: If something doesn’t look right in your document, and you need to make changes to the script, follow the editing procedure using Synapse outlined in Chapter 1.

 

Quick tips icon.

  • Just as creating a cookie does not take effect until another page is loaded, deleting a cookie does not take effect until another page. This is to say that you can delete a cookie on a page but still access that cookie on it (because the cookie was received by the page before the delete cookie was sent).
  • Just as creating cookies has mixed results using different browsers, the same applies to deleting them. Test your scripts on many browsers and play with the setcookie() settings to ensure the best overall compatibility.
  • In the remainder of this chapter you will use sessions to create a login/logout application. Using the cookie techniques demonstrated so far, you could also create a cookie-based login/logout feature using the same sample scripts as your basis. But attempt this, only if you have ample time!

Return to Chapter 9, Assignments Page