PHP Setcookie value null or empty string Delete cookie parsing _php tutorial

Source: Internet
Author: User
Tags setcookie
Many friends use the Setcookie setting value is empty or null when the system will automatically delete the cookie, let me give you to analyze the cookie value is null or empty string Delete cookie reason, there are need to learn friends can refer to.

This is also the case in the official documentation:


#2 Setcookie () Delete Example

The code is as follows Copy Code

Set the expiration date to one hour ago
Setcookie ("TestCookie", "", Time ()-3600);
Setcookie ("TestCookie", "", Time ()-3600, "/~rasmus/", "example.com", 1);
?>

Today encountered a strange thing, in the Setcookie, passed an empty string to $value, the result is this cookie was deleted ...

The code is as follows Copy Code

$name = "Post_url";
$value = "";
Setcookie ($name, $value, Time () +60*60*3, "/");

To go through PHP 5.4.13 source of the results learned

The type of value in the parameter in the C language is char *, and a value_len indicates its length.
If Value_len is 0, the following cookie is written:
The value is "deleted", the expiration Time is Thu, 01-jan-1970 08:00:01 CST or Thu, 01-jan-1970 00:00:01 GMT

It seems that Setcookie ($name, "") can actually delete this cookie ...
Similarly, in PHP, strval (null) = = = "", so Setcookie ($name, NULL) is also equivalent to Setcookie ($name, ""), which can also delete this cookie.

http://www.bkjia.com/PHPjc/632123.html www.bkjia.com true http://www.bkjia.com/PHPjc/632123.html techarticle Many friends use the Setcookie setting value is empty or null when the system will automatically delete the cookie, let me give you to analyze the cookie value is null or empty string Delete cookie reason, it is necessary ...

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