Open In App

How to Remove a Cookie in PHP?

Last Updated : 10 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Next Article
Article Tags :

Similar Reads