How to delete a cookie in php

Source: Internet
Author: User
Effective methods for deleting cookies in php & nbsp; effective methods for deleting cookies in php instructions for deleting cookies start ----- boolsetcookie (stringname [, stringvalue [, intexpire [, stringpath [, stringdomain [, boolsecure]) how to delete cooki php

Php effective method for deleting cookies
Instructions for deleting cookies begin -----

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:
// Set the expiration time to one hour ago
Setcookie ("TestCookie", "", time ()-3600 );
Setcookie ("TestCookie", "", time ()-3600 ,"/~ Rasmus/",". utoronto. ca ", 1 );
?>


----- Description about cookie deletion ends -----

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:
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:
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.

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

Feimengv on the first floor
Open source code is refreshing

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.