PHP Delete Cookie effective method, PHP delete cookie_php tutorial

Source: Internet
Author: User

PHP Delete Cookie effective method, PHP Delete cookie


PHP effective way to delete cookies
Instructions for deleting a cookie start-----

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 period is in the past before it triggers the browser's removal mechanism.

The following example shows how to delete a cookie that you just set:
Set the expiration time to one hour ago
Setcookie ("TestCookie", "", Time ()-3600);
Setcookie ("TestCookie", "", Time ()-3600, "/~rasmus/", ". utoronto.ca", 1);
?>


-----The end of the instructions for deleting cookies-----

The way to delete a cookie is to set the cookie's validity period to the current time, which is what almost any PHP programmer would do.

Later, a friend of my first contact with PHP told me that in the program he had wanted to set the value of a cookie to null, resulting in the cookie being deleted directly. My first reaction was not to believe, so the test
A moment:
Setcookie ("TestCookie", "');
Print_r ($_cookie);


The result is that the entire $_cookie array is empty, not just $_cookie[' TestCookie '). So with Winsock grab packet, observe the return HTTP header, found HTTP header unexpectedly is "set-cookie:testcookie=deleted;" Expires=mon, 18-jun-2007 02:42:33 GMT ", which shows" Setcookie ("TestCookie", "');" It is true that TestCookie is deleted directly from the cookie, which is completely not explained in the PHP manual.

Finally read the PHP source code, finally found the truth (this is the benefits of open source, what is unclear inside, directly check the source).

The following code can be found in the php5.20 Linux source package near line 99th EXT/STANDARD/HEAD.C:
if (value && Value_len = = 0) {
/*
* MSIE doesn ' t delete a cookie when you set it to a null value
* So-in order-to-force cookie to is deleted, even on MSIE, we
* Pick an expiry date 1 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 o'clock, "sprintf (Cookie," Set-cookie:%s=deleted; expires=%s ", name, DT);" The HTTP header that deletes the cookie is sent to the browser.

Finally, we can conclude that "Setcookie ($cookiename,") is used in PHP; or "Setcookie ($cookiename, NULL);" Will delete cookies, of course, not in these manuals.

Source: http://www.111cn.net/phper/21/f0eace11b1229a0f2c7c54e3c1ea4654.htm

http://www.bkjia.com/PHPjc/918673.html www.bkjia.com true http://www.bkjia.com/PHPjc/918673.html techarticle PHP Delete Cookie effective method, PHP Delete cookie effective method of PHP Delete cookie The description of the deletion cookie begins-----bool Setcookie (string name [, string value [, int expire [, str ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.