Delete a cookie if the phpsetcookie value is null or an empty string _ PHP Tutorial

Source: Internet
Author: User
The phpsetcookie value is null or an empty string is used to delete cookie parsing. Many of my friends will automatically delete the cookie when using setcookie to set the value to NULL or null. let me analyze the reason why the cookie value is NULL or a null string to delete the cookie, when setcookie is set to NULL, the system automatically deletes the cookie, next, let me analyze the reason why the cookie value is null or an empty string is used to delete the cookie. For more information, see.

This is also written in the official document:


#2 setcookie () delete example

The code is as follows:

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

Today, I encountered a strange thing. when I set the cookie, I sent an empty string to $ value. The result was that the cookie was deleted...

The code is as follows:

$ Name = "post_url ";
$ Value = "";
Setcookie ($ name, $ value, time () + 60*60*3 ,"/");

Go to php 5.4.13 and find out the source code.

The value type in the parameter is char * in C, and value_len indicates its length.
If value_len is 0, the following cookie is written:
The value is "deleted" and 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 indeed delete this cookie...
Similarly, in php, strval (NULL) = "", so setcookie ($ name, NULL) is equivalent to setcookie ($ name, ""), you can also delete this cookie.

...

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.