An in-depth explanation of PHP removal Cookie tips _php Tutorial

Source: Internet
Author: User
Tags setcookie sprintf

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.

 
 
  1. if (value && value_len = = 0) {
  2. /*
  3. * MSIE doesn ' t delete a cookie when
    You set it to a null value
  4. * So-in order-force cookie to be
    deleted, even on MSIE, we
  5. * Pick an expiry date 1 and 1
    Second in the past
  6. */
  7. time_t T = Time (NULL)-31536001;
  8. 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);
  9. sprintf (Cookie, "Set-cookie:
    %s=deleted; Expires =%s ", name, DT);
  10. Efree (DT);
  11. } else {
  12. sprintf (Cookie, "Set-cookie:%s=%s",
    Name, value? Encoded_value: "");
  13. if (expires > 0) {
  14. strcat (Cookie, "; Expires =");
  15. 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);
  16. strcat (cookie, DT);
  17. Efree (DT);
  18. }
  19. }

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

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