Ec (2); php effective methods for deleting cookies instructions for deleting cookies begin ----- boolsetcookie (stringname [, stringvalue [, intexpire [, stringpath [, stringdomain [, boolsecure]) to delete a cookie, you must ensure that its expiration time is in the past to trigger the deletion mechanism of the browser. Script ec (2); script
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.