php setcookie時值為null或Null 字元串(刪除cookie)_PHP教程

來源:互聯網
上載者:User
在php中設定cookie與刪除cookie都可以使用php setcookie來實現,如果設定就設定有值,如果刪除就設定cookie value為空白或null或時間到期都可以刪除,下面我們來看一些執行個體。

長久以來,在php中刪除cookie的時候,都是使用
bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string

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

$value 隨便寫, $expire設定為一個已經過去的時間即可。

官方文檔中也是這樣寫的:

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:

代碼如下 複製代碼
// set the expiration date to one hour ago
setcookie ("TestCookie", "", time() - 3600);
setcookie ("TestCookie", "", time() - 3600, "/~rasmus/", "example.com", 1);
?>

今天遇到一件奇怪的事, 在setcookie的時候,傳了一個Null 字元串給$value,結果竟然是此cookie被刪除了

代碼如下 複製代碼

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

delete_cookie

相當不解。

去翻php 5.4.13 的源碼:

ext/standard/head.c

代碼如下 複製代碼

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) != ' ') {

參數中的value在C語言中的類型是char * , 還有一個 value_len標明了它的長度。
如果value_len為0的話,就寫了下面的cookie:
值為”deleted”, 到期時間為 Thu, 01-Jan-1970 08:00:01 CST 或者說是 Thu, 01-Jan-1970 00:00:01

GMT

看來setcookie($name, “”) 確實可以刪除這個cookie了…
同理,在php中,strval(NULL) === “” , 所以 setcookie($name, NULL) 也就相當於 setcookie($name,

“”),同樣可以刪除此cookie.

另外,比較好奇的是:

代碼如下 複製代碼
if (value && value_len == 0) {
}
else {
}

else 中包含了 value 為null 的情況, 這是一種什麼樣的情況呢?

看來setcookie($name, “”) 確實可以刪除這個cookie了…
同理,在php中,strval(NULL) === “” , 所以 setcookie($name, NULL) 也就相當於 setcookie($name,

“”),同樣可以刪除此cookie.

另外,比較好奇的是:

代碼如下 複製代碼
if (value && value_len == 0) {
}
else {
}

else 中包含了 value 為null 的情況, 這是一種什麼樣的情況呢?

http://www.bkjia.com/PHPjc/632738.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/632738.htmlTechArticle在php中設定cookie與刪除cookie都可以使用php setcookie來實現,如果設定就設定有值,如果刪除就設定cookie value為空白或null或時間到期都可以刪除,...

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.