The php setcookie value is null or a null string (delete cookie)

Source: Internet
Author: User
Tags cookie names setcookie

For a long time, when deleting cookies in php
Bool setcookie (string $ name [, string $ value [, int $ expire = 0 [, string $ path [, string

$ Domain [, bool $ secure = false [, bool $ httponly = false])

$ Value can be written at will, and $ expire can be set to a time that has passed.

This is also written in the official document:

Http://www.php.net/manual/en/function.setcookie.php

Example #2 setcookie () delete example
When deleting a cookie you shoshould assure that the expiration date is in the past, to trigger

The removal mechanic in your browser. Examples follow how to delete cookies sent in previous

Example:

The code is as follows: Copy code
<? Php
// 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 passed an empty string to $ value, and the result was that the cookie was deleted.

...

The code is as follows: Copy code

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

Delete_cookie

Rather puzzled.

Go to php 5.4.13 source code:

Ext/standard/head. c

The code is as follows: Copy code

173 PHP_FUNCTION (setcookie)
174 {
175 char * name, * value = NULL, * path = NULL, * domain = NULL;
176 long expires = 0;
177 zend_bool secure = 0, httponly = 0;
178 int name_len, value_len = 0, path_len = 0, domain_len = 0;
179
180 if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "s | slssbb", & name,
181 & name_len, & value, & value_len, & expires, & path,
182 & path_len, & domain, & domain_len, & secure, & httponly) =

FAILURE ){
183 return;
184}
185
186 if (php_setcookie (name, name_len, value, value_len, expires, path, path_len, domain,

Domain_len, secure, 1, httponly TSRMLS_CC) = SUCCESS ){
187 RETVAL_TRUE;
188} else {
189 RETVAL_FALSE;
190}
191}
 
 
 
76 PHPAPI int php_setcookie (char * name, int name_len, char * value, int value_len, time_t

Expires, char * path, int path_len, char * domain, int domain_len, int secure, int url_encode,

Int httponly TSRMLS_DC)
77 {
78 char * cookie, * encoded_value = NULL;
79 int len = sizeof ("Set-Cookie :");
80 char * dt;
81 sapi_header_line ctr = {0 };
82 int result;
83
84 if (name & strpbrk (name, "=,; trn1314 ")! = NULL) {/* man isspace for 13

And 14 */
85 zend_error (E_WARNING, "Cookie names cannot contain any of the following '= ,;

\ T \ r \ n \ 013 \ 014 '");
86 return FAILURE;
87}
88
89 if (! Url_encode & value & strpbrk (value, ",; trn1314 ")! = NULL) {/* man

Isspace for 13 and 14 */
90 zend_error (E_WARNING, "Cookie values cannot contain any of the following ',;

\ T \ r \ n \ 013 \ 014 '");
91 return FAILURE;
92}
93
94 len + = name_len;
95 if (value & url_encode ){
96 int encoded_value_len;
97
98 encoded_value = php_url_encode (value, value_len, & encoded_value_len );
99 len + = encoded_value_len;
100} else if (value ){
101 encoded_value = estrdup (value );
102 len + = value_len;
103}
104 if (path ){
105 len + = path_len;
106}
107 if (domain ){
108 len + = domain_len;
109}
110
111 cookie = emalloc (len + 100 );
112
113 if (value & value_len = 0 ){
114 /*
115 * MSIE doesn't delete a cookie when you set it to a null value
116 * so in order to force cookies to be deleted, even on MSIE, we
117 * pick an expiry date in the past
118 */
119 dt = php_format_date ("D, d-M-Y H: I: s T", sizeof ("D, d-M-Y H: I: s T")-1, 1, 0

TSRMLS_CC );
120 snprintf (cookie, len + 100, "Set-Cookie: % s = deleted; expires = % s", name, dt );
121 efree (dt );
122} else {
123 snprintf (cookie, len + 100, "Set-Cookie: % s = % s", name, value? Encoded_value:

"");
124 if (expires> 0 ){
125 const char * p;
126 strlcat (cookie, "; expires =", len + 100 );
127 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 );
128/* check to make sure that the year does not exceed 4 digits in length */
129 p = zend_memrchr (dt, '-', strlen (dt ));
130 if (! P | * (p + 5 )! = ''){

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.

In addition, I'm curious about the following:

The code is as follows: Copy code
If (value & value_len = 0 ){
}
Else {
}

Else contains the case where value is null. What is the case?

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.

In addition, I'm curious about the following:

The code is as follows: Copy code
If (value & value_len = 0 ){
}
Else {
}

Else contains the case where value is null. What is the case?

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.