How to Remove a Cookie in PHP?
Last Updated :
10 Sep, 2024
Cookies are small text files stored in a user's browser. It contain data such as user preferences, session information, and other relevant details. PHP cookies are used to store information that can persist across different web pages or sessions. At times, you may need to delete or remove a cookie, for instance, to log out a user or reset their preferences. In this article, we will cover how to remove a cookie in PHP.
What is a Cookie in PHP?
A cookie is a small piece of data that is stored on the client-side (browser) and sent to the server with each HTTP request. PHP provides the setcookie() function to create, modify, and delete cookies.
- Setting a Cookie: Stores data in the user's browser.
- Reading a Cookie: Allows you to retrieve stored data in future requests.
- Deleting a Cookie: Involves "unsetting" the cookie by modifying its expiration time.
Creating/Setting a Cookie in PHP
To create and set the cookie, you can use the setcookie() function of the PHP.
Syntax
setcookie( name, value, expire, path, domain, secure, httponly )
Parameters: This function accepts seven parameters as mentioned above and described below:
- name: The name of the cookie.
- value: The value you want to store in the cookie.
- expire-time: It is the number of seconds until the cookie will be kept on the user's machine by the browser. After that, it will automatically be deleted. If not set then the cookie will be preserved by browser until it is open.
- path: It determines for which directories cookie will valid. If you want to access it in all directories then put it on "/", i.e. the cookie is accessible in the entire domain. Otherwise, the cookie will be limited to the subdirectory.
- domain: It is used to define the access hierarchy for the cookie. For example, if you set this to "yourdomain.com", it will be accessible via all the subdomains also. but if it set to "sub.yourdomain.com", it will be accessible by "sub.yourdomain.com" and its subdomains.
- secure: It determines how the cookie will be sent, via HTTP or HTTPS. If set to true then the cookie will be sent via HTTPS only, otherwise, it will be sent via HTTP. Its default value is false.
- httponly: If it set to true, the cookie is accessible only either via HTTP or HTTPS. That means the client code (like Javascript) can not access the cookie.
Out of the above parameters, only the first two parameters are mandatory. Others are optional parameters. If you want to preserve the cookie, then provide the expire-time parameter.
Note: It is stored in the global array named $_COOKIE.
PHP
<!DOCTYPE html>
<?php
$cookie_name = "gfg";
$cookie_value = "GeeksforGeeks";
// 1 day = 86400 Seconds
setcookie($cookie_name, $cookie_value, time() + (86400 * 15), "/");
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
}
else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
Output
Cookie 'gfg' is set!
Value is: GeeksforGeeks
How to Remove a Cookie in PHP?
To remove or delete a cookie in PHP, you cannot directly "delete" it like you would a variable. Instead, you need to:
- Set the cookie's expiration time to a point in the past.
- Ensure that other parameters (name, path, domain) match exactly as they were when the cookie was set.
- When the browser sees that the cookie has an expiration time in the past, it automatically removes it from the client-side storage.
Syntax for Removing a Cookie
To remove a cookie, use the setcookie() function with the same parameters as when you created it, but with an expiration time that has already passed (e.g., time() - 3600).
setcookie(name, time() - 3600);
Example
PHP
<!DOCTYPE html>
<?php
// Set the expiration date to one hour ago
setcookie("gfg", "", time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'gfg' is deleted.";
?>
</body>
</html>
Output:
Cookie 'gfg' is deleted.
Note: The setcookie() function must appear before the <html> tag.
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
Similar Reads
How to Set Cookie in ReactJS ?
Cookies in React are used to set value in a key-value form which can be set in browser storage and used further for identifying the current user. The following example shows how to set the cookie in the ReactJS application, here we have taken the custom-cookie as key, which is set in the cookie with
3 min read
How to reset Array in PHP ?
You can reset array values or clear the values very easily in PHP. There are two methods to reset the array which are discussed further in this article. Methods: unset() Functionarray_diff() Function Method 1: unset() function: The unset() function is used to unset a specified variable or entire arr
2 min read
How to remove HTML tags from data in PHP ?
Removing HTML tags from data in PHP is a crucial step for sanitizing user input or displaying content safely. This process involves using the strip_tags() function to eliminate any HTML or PHP tags from a string, leaving only plain text. It's essential for preventing potential security risks, such a
1 min read
How to Access HTTP Cookie in Node.js ?
Cookies are small pieces of data sent by a server and stored on the client side, typically in the user's browser. They are often used to maintain stateful information such as user sessions, preferences, or tracking data. In Node.js, accessing and managing cookies is a common requirement for building
3 min read
How to Get Cookie by Name in JavaScript?
Getting a specific name in JavaScript involves parsing the document's cookie string contains all cookies in a single string separated by semicolons and space. The goal is to extract the value of a specific cookie by the name given. The Cookie is defined as a small piece of data that is stored on the
4 min read
How to create and destroy cookies in PHP ?
Cookies are used to store the user information in the browser. If you store the cookie in the browser then every time you logged in to the browser then you can log in with the help of stored user information. We can understand this by saying when a user sends the request to the server then the cooki
3 min read
How to Set Cookie in Ruby on Rails?
Cookies are small pieces of data that are sent from a website and stored on the user's computer by the user's web browser. They are commonly used for session management, user authentication, personalization, and tracking. In Ruby on Rails, you can use the 'cookies' object to set, read, and delete co
3 min read
PHP | DsMap remove() Function
The Ds\Map::remove() function is an inbuilt function in PHP which is used to remove and return a value by key. Syntax: mixed Ds\Map::remove( $key, $default ) Parameters: This function accepts two parameters as mentioned above and described below: $key: It holds the key value which need to remove. $d
2 min read
How to add 301 redirects in PHP ?
In this article, we are going to discuss how to add 301 redirects in PHP. Sometimes we come across scenarios like a few web pages redirecting from one page to another page without clicking/doing anything. This can be done by developers if they want users to use their updated version of their webpage
3 min read
How to use setcookie() function in PHP ?
A cookie is often a small file that is embedded by the server from which the user has visited or is getting a response. Each time the computer requests a page within a browser, it will send a cookie. Using PHP we can do both create and retrieve cookie values. A variable is automatically created with
3 min read