How to delete cookies in php
In PHP, you can use the setcookie() function to delete cookies. You only need to set the second parameter of the function to empty, or set the third parameter to be less than the current time of the system. Syntax "setcookie($cookiename,'');", "setcookie($cookiename, NULL);" or "setcookie($cookiename, "", time() - 3600);".
The operating environment of this article: Windows 7 system, PHP 8 version, DELL G3 computer.
php Delete cookies
Let’s first look at the mechanism of related cookies.
bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, bool secure]]]]] )
To delete a cookie, you need to ensure that its expiration date is in the past to trigger the browser's deletion mechanism.
The following example illustrates how to delete the cookie just set:
//将过期时间设为一小时前 setcookie("TestCookie", "", time() - 3600); setcookie("TestCookie", "", time() - 3600, "/~rasmus/", ".utoronto.ca", 1); ?>
The way to delete a cookie is to set the validity period of the cookie to before the current time. This is also what almost all PHP programmers do. Do this.
Later, a friend who was new to PHP told me that he wanted to set the value of a cookie to empty in the program, but the cookie was deleted directly. My first reaction at the time was that I didn't believe it, so I tested it:
setcookie("testcookie", ''); print_r($_COOKIE);
The result was that the entire $_COOKIE array was empty, not just $_COOKIE['testcookie']. So I used winsock to capture the packet and observed the returned http header. I found that the http header turned out to be "Set-Cookie: testcookie=deleted; expires=Mon, 18-Jun-2007 02:42:33 GMT", which means "setcookie("testcookie ", '');" indeed deletes the cookie testcookie directly, and there is no explanation at all in the PHP manual about this situation.
Finally read the PHP source code and finally discovered the truth (this is the benefit of open source, if there is any unclear inside story, just check the source code directly).
The following code can be found near line 99 of ext/standard/head.c in the php5.20 Linux source package:
if (value && value_len == 0) { /* * MSIE doesn't delete a cookie when you set it to a null value * so in order to force cookies to be deleted, even on MSIE, we * pick an expiry date 1 year and 1 second in the past */ time_t t = time(NULL) - 31536001; dt = php_format_date("D, d-M-Y H:i:s T", sizeof("D, d-M-Y H:i:s T")-1, t, 0 TSRMLS_CC); sprintf(cookie, "Set-Cookie: %s=deleted; expires=%s", name, dt); efree(dt); } else { sprintf(cookie, "Set-Cookie: %s=%s", name, value ? encoded_value : ""); if (expires > 0) { strcat(cookie, "; expires="); dt = php_format_date("D, d-M-Y H:i:s T", sizeof("D, d-M-Y H:i:s T")-1, expires, 0 TSRMLS_CC); strcat(cookie, dt); efree(dt); } }
The source code clearly shows "if (value && value_len == 0)", when "value_len" is 0, "sprintf(cookie, "Set-Cookie: %s=deleted; expires=%s", name, dt);" will send the http header to delete the cookie. browser.
Finally we can draw the conclusion: using "setcookie($cookiename, '');" or "setcookie($cookiename, NULL);" in php will delete cookies, but of course there is no such manual.
(Recommended: "PHP Video Tutorial")
The above is the detailed content of How to delete cookies in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
