PHP Remove Cookies

In this chapter you will learn:

  1. How to delete a cookie
  2. Example - delete a cookie
  3. Note for cookie delete

How

To delete a cookie, call setcookie() with the cookie name and any value such as an empty string), and pass in an expires argument that is in the past.

Example

You should pass exactly the same path, domain, and other fields that you used when you first created the cookie to ensure that the correct cookie is deleted:

setcookie( "fontSize", "", time() - 3600, "/", ".example.com", false, true );

This example sets the fontSize cookie's expiry time to one hour in the past, which effectively deletes it from the browser.

Note

As with creating and updating cookies, deleting a cookie via setcookie() doesn ' t delete it from the $_COOKIE array while the script is running.

However, the next time the browser visits the page, it will no longer send the cookie to the server and the corresponding $_COOKIE array element will not be created.

Next chapter...

What you will learn in the next chapter:

  1. Using PHP Sessions to Store Data
  2. Where the session file stores
  3. Creating a Session
  4. Example - Access session data
Home » PHP Tutorial » PHP Cookie
PHP Cookies
PHP Cookie Components
PHP setcookie() function
PHP Access Cookies
PHP Remove Cookies