First, let's take a look at the PHP manual for instructions on removing cookies from PHP.
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: Example 2. Setcookie () Delete
Example
Set the expiration time to one hour ago
Setcookie ("TestCookie", "", Time ()-3600);
Setcookie ("TestCookie", "", Time ()-3600, "/~rasmus/", ". utoronto.ca", 1);
The way PHP removes cookies is to set the cookie's validity period to the current time, which is what almost all PHP programmers will 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 I tested it.
Setcookie ("TestCookie", "');
Print_r ($_cookie);
The result is that the entire $_cookie array is empty, not just $_cookie[' TestCookie '] empty. So I grabbed the packet with Winsock, observed the HTTP header that was returned, and found that the HTTP header was set-cookie:testcookie= Deleted Expires=mon, 18-jun-2007 02:42:33 GMT. This shows that Setcookie ("TestCookie", "the"), is indeed the TestCookie this cookie is deleted directly. And this is not explained in the PHP manual.
Finally read the PHP delete cookie source code, finally found the truth (this is the benefits of open source, what is unclear in the insider directly check the source)
The following PHP deletion cookie 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-force cookie to be
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 is clearly displayed, if (value && Value_len = = 0), when Value_len is 0 o'clock
sprintf (Cookie, "Set-cookie:%s=deleted; expires=%s ", name, DT);
will send PHP to delete the HTTP header of the cookie to the browser. Finally, we can conclude that using PHP
Setcookie ($cookiename, "); or Setcookie ($cookiename, NULL);
Will implement PHP delete cookies, of course, not in these manuals.
http://www.bkjia.com/PHPjc/445947.html www.bkjia.com true http://www.bkjia.com/PHPjc/445947.html techarticle first, let's take a look at the PHP manual about PHP Delete Cookie description bool Setcookie (string name [, string value [, int expire [, String path [, String DOMA In [, BOOL secure ]]]]) to delete ...