PHP Setcookie value is null or an empty string (delete cookie) _php tutorial

Source: Internet
Author: User
Tags cookie names set cookie setcookie
Set the cookie in PHP and delete cookies can be implemented using PHP Setcookie, if set to set a value, if the deletion of the set cookie value is null or NULL or time expires can be deleted, let's look at some examples.

For a long time, when you delete cookies in PHP, you are using
BOOL Setcookie (String $name [, String $value [, int $expire = 0 [, String $path [, String

$domain [, bool $secure = False [, bool $httponly = false]]] []])

$value casually write, $expire set to a time that has elapsed.

This is also the case in the official documentation:

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

Example #2 setcookie () Delete Example
When deleting a cookie should assure, the expiration date was in the past, to trigger

The removal mechanism in your browser. Examples follow how to delete cookie sent in previous

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, I 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, "/");

Delete_cookie

Quite puzzled.

To go through the 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
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}



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 {
*cookie char, *encoded_value = NULL;
len=sizeof int ("Set-cookie:");
*dt Char;
Bayi Sapi_header_line ctr = {0};
The result of a.
83
if (name && strpbrk (name, "=,; trn1314 ")! = NULL) {/* man isspace for 13

and 14 */
Zend_error (e_warning, "Cookie names cannot contain any of the following ' =,;

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

Isspace for and 14 * *
Zend_error (e_warning, "Cookie values cannot contain any of the following ',;

\t\r\n\013\014 ' ");
FAILURE return;
92}

94 Len + = Name_len;
if (value && url_encode) {
$ int encoded_value_len;
+
98 en Coded_value = Php_url_encode (value, Value_len, &encoded_value_len);
Len + encoded_value_len;
+ Else if (value) {
101 Encoded_value = estrdup (value);
102 Len + = Value_len;
103}
104 if (path) {
. len + + Path_len;
106}
107 if (domain) {
108 len + domain_len;
109}
111
cookie = emalloc (len +);

113 if (value && Value_len = = 0) {
* *
* MSIE doesn ' t delete a cookie when you set it to a n ULL value
$ * In order to force cookie to is deleted, even on MSIE, we
117 * Pick a expiry date in the past118 */
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);
snprintf (Cookie, Len +, "Set-cookie:%s=deleted; expires=%s ", name, DT);
121 efree (DT);
122} else {
123 snprintf (Cookie, Len +, "Set-cookie:%s=%s", name, value? Encoded_value:

"");
124 if (Expires > 0) {
the 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);
*/* Check to make sure, the year does not exceed 4 digits in length */
129 p = zend_memrchr (dt, '-', strlen (DT));
if (!p | | * (p + 5)! = ") {

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 equivalent to Setcookie ($name,

""), you can also delete this cookie.

In addition, the more curious is:

The code is as follows Copy Code
if (value && Value_len = = 0) {
}
else {
}

If else contains the value null, what is the case?

It seems that Setcookie ($name, "") can actually 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, the more curious is:

The code is as follows Copy Code
if (value && Value_len = = 0) {
}
else {
}

If else contains the value null, what is the case?

http://www.bkjia.com/PHPjc/632738.html www.bkjia.com true http://www.bkjia.com/PHPjc/632738.html techarticle setting cookies and deleting cookies in PHP can be implemented using PHP Setcookie, if the settings are set to a value, if you delete the Set cookie value is empty or null or time expires can be deleted, ...

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