Home > Backend Development > PHP Tutorial > php setcookie value is null or empty string to delete cookie parsing_PHP tutorial

php setcookie value is null or empty string to delete cookie parsing_PHP tutorial

WBOY
Release: 2016-07-13 10:57:03
Original
1060 people have browsed it

Many friends use setcookie to set the value to be empty or NULL. The system will automatically delete the cookie. Let me analyze the reasons why the cookie value is null or empty string to delete the cookie. Friends who need to learn can refer to it. .

This is also written in the official documentation:


#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);
?>

Copy code

 代码如下 复制代码

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

// set the expiration date to one hour ago

setcookie ("TestCookie", "", time() - 3600);

setcookie ("TestCookie", "", time() - 3600, "/~rasmus/", "example.com", 1);
?>


I encountered a strange thing today. When setting cookie, I passed an empty string to $value, but the result turned out to be that the cookie was deleted...
The code is as follows
Copy code

$name = "post_url";

$value = ""; Go through the source code of php 5.4.13 and get the result The type of value in the parameter is char * in C language, and there is also a value_len indicating 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, ""), and this cookie can also be deleted.
http://www.bkjia.com/PHPjc/632123.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632123.htmlTechArticleMany friends use setcookie to set the value to empty or NULL. The system will automatically delete the cookie. Here I will Let me analyze the reasons for deleting cookies when the cookie value is null or empty string. If necessary...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template