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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read