How to destroy or remove a cookie in PHP

PHP: How to destroy or remove a cookie in PHP

To remove a cookie, you should set its expiry to a date in the past.

  1. setcookie("the_cookie_name", "", time()-(60*60*24*7));
  2. // or also set the path, if it was set with a path:
  3. setcookie("the_cookie_name", "", time()-(60*60*24*7),"/");

And then the next time the user loads a page (or makes a request), their browser will remove the cookie.

You should also clear PHP's 'copy' of the cookie:

  1. unset($_COOKIE["the_cookie_name"]);

webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about How to destroy or remove a cookie in PHP

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the PHP language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to enable the rewrite engine in .htaccess?

How to output (echo/print) content in PHP

How do you run a PHP script from the command line?

How to check if an object or variable is an array, in JS?

How to block access to certain referrers in Apache using .htaccess

How to check if two strings are anagrams in PHP?

How to generate an array for a dropdown (with ['key' => 'value']) suitable for a dropdown, from a Laravel collection of Eloquent objects?

How to use Anonymous classes in PHP 7?

How to fix the MySQL related "Specified key was too long error" error in Laravel

How to find an item by Primary Key in Eloquent, or throw a ModelNotFoundException exception if it isn't found?