When php setcookie the value is null or empty string (delete cookie)_PHP tutorial

WBOY
Release: 2016-07-13 10:49:03
Original
1171 people have browsed it

Setting cookies and deleting cookies in php can be achieved using php setcookie. If it is set, it will be set with a value. If it is deleted, it will be set. The cookie value can be deleted if it is empty or null or the time has expired. Let's look at some examples below.

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

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

Write $value as you like, and set $expire to a time that has passed.

This is also written in the official documentation:

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

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

the removal mechanism in your browser. Examples follow how to delete cookies 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);
?>
// 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
 代码如下 复制代码

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

delete_cookie

The code is as follows Copy code
$name = "post_url"; $value = ""; setcookie($name, $value, time()+60*60*3, "/" ); delete_cookie

Quite puzzled.

Go to the source code of php 5.4.13:

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 '=,;

trn 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 ',;

trn

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