To delete a cookie in php, you can use the php setcookie() function with an expiration time which is in the past. It is also good practice to unset the variable with unset() from the $_COOKIE super global variable.

setcookie("cookie", "", time() - 3600);

When working with web pages in php, many times we are working with cookies so that we can store information about a user. Cookies are a way to store data in specific to a user in their browser.

It’s important to be able to set and remove cookies easily, and with php, we can easily delete a cookie.

To set and remove cookies in php, you can use the php setcookie() function.

The php setcookie() function takes three required parameters: the cookie name, the cookie value, and the expiration date.

To remove a cookie in our php code, we should pass an expiration date which is in the past so the cookie expires right away.

Below is some sample code in php which shows how to get rid of a cookie.

setcookie("cookie", "", time() - 3600);

It is good practice to also remove the cookie from the $_COOKIE super global variable. To remove the cookie from $_COOKIE, we can use the php unset() function.

By unsetting the cookie in $_COOKIE, we make sure that the cookie is not accessible later in the code.

setcookie("cookie", "", time() - 3600);

Hopefully this article has been useful for you to learn how to delete cookies in php.

Categorized in:


Last Update: March 13, 2024