Home > Backend Development > PHP Problem > How to delete cookies using php setcookie

How to delete cookies using php setcookie

藏色散人
Release: 2023-03-13 14:10:02
Original
2425 people have browsed it

php setcookie method to delete cookies: 1. Create a PHP sample file; 2. Delete a cookie through the "setcookie("TestCookie", "", time() - 3600);" method.

How to delete cookies using php setcookie

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

php setcookie How to delete cookies?

php Detailed explanation of how to delete cookies:

Let’s first look at the mechanism of related cookies.

The code is as follows:

bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, bool secure]]]]] )
Copy after login

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:

The code is as follows:

<?php
//将过期时间设为一小时前
setcookie("TestCookie", "", time() - 3600);
setcookie("TestCookie", "", time() - 3600, "/~rasmus/", ".utoronto.ca", 1);
?>
Copy after login

The way to delete a cookie is to set the validity period of the cookie to Before the current time, this was what almost all PHP programmers would do.

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
:

The code is as follows:

setcookie("testcookie", &#39;&#39;);
print_r($_COOKIE);
Copy after login

The result is that the entire $_COOKIE array is empty, and Not just $_COOKIE['testcookie'] is empty. 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:

The code is as follows:

if (value && value_len == 0) {
    /* 
     * MSIE doesn&#39;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);
    }
}
Copy after login

Clear in the source code Clearly display "if (value && value_len == 0)", when "value_len" is 0, "sprintf(cookie, "Set-Cookie: %s=deleted; expires=%s", name, dt); ” will send an http header to delete the cookie to the 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.

Isn’t it very simple? Sometimes we still need to read the php source code carefully.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to delete cookies using php setcookie. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template