How to Delete Cookies in PHP

Subodh Poudel Feb 02, 2024
  1. Use the setcookie() Function to Set Cookies in PHP
  2. Use the setcookie() Function to Delete Cookies in PHP
How to Delete Cookies in PHP

This article will introduce a few methods to delete cookies in PHP. Firstly, we will set the cookies and then delete them.

Use the setcookie() Function to Set Cookies in PHP

The setCookie() function sets and deletes the cookies.

Syntax:

setcookie($name, $value, $expiry, $path, $domain, $secure, $httponly);
  • $name is the name of the cookie.
  • $value is the content or the value of the cookie stored in the client’s system.
  • $expiry is the time when the cookie expires. The unit is second.
  • $path is the path on the server that denotes the availability of cookies. For example, if the $path is set to /, the cookie will be available within the website’s domain.
  • $domain is the domain or the subdomain where the cookie is available.
  • $secure ensures that the cookie is transmitted over a secure HTTPS connection from the client when set to true.
  • $httponly ensures that the cookie is accessible only through the HTTP protocol when set to true.

Let’s create a cookie and the variables $cookie_name, $cookie_value, and $cookie_expiry to store the cookie name, value, and expiry time.

Assign username, Sam and time()+3600 for each of the variables. Next, use the setcookie() method and supply these variables in order.

Next, use the global variable $_COOKIE to access the $cookie_name.

$cookie_name = "username";
$cookie_value = "Sam";
$cookie_expiry = time() + 3600;

setcookie($cookie_name, $cookie_value, $cookie_expiry);
print_r($_COOKIE[$cookie_name]);

Output:

Sam

Thus, we have created a cookie. The cookie expires after one hour from its creation time as 3600 seconds is equivalent to one hour.

We can see the cookies information in the browser from the inspect element section. Now, we will walk through deleting the cookies in the sections below.

Use the setcookie() Function to Delete Cookies in PHP

Use the setcookie() method to delete the cookies. For that, we need to keep the expiry date of the past.

We can use the isset() function to check if the cookie has been set before deleting the cookie.

For example, use the $_COOKI[$cookie_name] variable in the isset() function to check if the above created cookie exist.

In the if block, use the setcookie() function. Inside the function, set $cookie_name as the first parameter.

if(isset($_COOKIE[$cookie_name])) {
 setcookie($cookie_name, "", time()-3600);
}

We wrote an empty string and time()-3600 for the expiry time for the second parameter.

As the time() function returns the current time in seconds since Epoch, 3600 subtracted from it will return the past time. Finally, the cookie with the $cookie_name is deleted.

We can also use $cookie_name and null value in the setcookie() function to delete the cookies.

if(isset($_COOKIE[$cookie_name])) {
 setcookie($cookie_name, null);
}

Thus, we can use the setcookie() function to delete cookies in PHP.

Subodh Poudel avatar Subodh Poudel avatar

Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.

LinkedIn