How php deletes cookies _ PHP-php Tutorial

Source: Internet
Author: User
This article briefly introduces how to delete a cookie in php, that is, to set the cookie validity period or to be empty. it is a very practical tips. we recommend it to you here. Let's first look at the cookie mechanism.

The code is as follows:


Bool setcookie (string name [, string value [, int expire [, string path [, string domain [, bool secure])

To delete a cookie, you must ensure that its expiration time is in the past to trigger the deletion mechanism of the browser.

The following example shows how to delete the cookie you just set:

The code is as follows:


<? Php
// Set the expiration time to one hour ago
Setcookie ("TestCookie", "", time ()-3600 );
Setcookie ("TestCookie", "", time ()-3600 ,"/~ Rasmus/",". utoronto. ca ", 1 );
?>

The method to delete a cookie is to set the cookie's validity period to before the current time, which is also done by almost all php programmers.

Later, a friend who first came into contact with php told me that he wanted to set the value of a cookie to null in the program. as a result, the cookie was directly deleted. I did not believe it at the time, so I tested it.
Below:

The code is as follows:


Setcookie ("testcookie ",'');
Print_r ($ _ COOKIE );

The result is that the entire $ _ COOKIE array is empty, rather than the $ _ COOKIE ['testcookie '] is empty. So we captured the packet with winsock, observed the returned http header, and found that the http header was "Set-Cookie: testcookie = deleted; expires = Mon, 18-Jun-2007 02:42:33 GMT ", this indicates that "setcookie (" testcookie ",''); "does delete the cookie directly, which is not described in the php Manual.

Finally, I read the php source code and finally found out the truth (this is the benefit of open source. if there is anything unclear, I can check the source code directly ).

The following code can be found near ext/standard/head. c 99th in php5.20 linux source code package:

The code is as follows:


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);" the http header used to delete the cookie is sent to the browser.

Finally, we can conclude that "setcookie ($ cookiename,''); "or" setcookie ($ cookiename, NULL); "in php will delete the cookie, of course these manuals do not exist.

Isn't it very easy? sometimes it is very necessary for us to read the php source code.

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.