How to Delete Cookies in PHP
-
Use the
setcookie()Function to Set Cookies in PHP -
Use the
setcookie()Function 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);
$nameis the name of the cookie.$valueis the content or the value of the cookie stored in the client’s system.$expiryis the time when the cookie expires. The unit is second.$pathis the path on the server that denotes the availability of cookies. For example, if the$pathis set to/, the cookie will be available within the website’s domain.$domainis the domain or the subdomain where the cookie is available.$secureensures that the cookie is transmitted over a secure HTTPS connection from the client when set totrue.$httponlyensures that the cookie is accessible only through theHTTPprotocol when set totrue.
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 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