Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How php setcookie deletes cookie

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "php setcookie how to delete cookie", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "php setcookie how to delete cookie" bar!

Php setcookie delete cookie method: 1, create a PHP sample file; 2, through "setcookie (" TestCookie ",", time ()-3600); "method to delete a cookie.

This article operating environment: Windows7 system, PHP7.1 version, DELL G3 computer

How does php setcookie delete cookie?

Php how to delete cookie is explained in detail:

Let's first take a look at the relevant mechanism of cookie.

The code is as follows:

Bool setcookie (string name [, string value [, int expire [, string path [, string domain [, bool secure])

To delete cookie, you need to make sure that its expiration date is in the past before you can trigger the browser's deletion mechanism.

The following example shows how to delete the cookie you just set:

The code is as follows:

The way to delete a cookie is to set the validity period of the cookie to before the current time, which is what almost all php programmers do.

Later, a friend who first came into contact with php told me that he wanted to set the value of a cookie to null in the program, but the cookie was deleted directly. My first reaction was not to believe it, so I tested it.

For a moment:

The code is as follows:

Setcookie ("testcookie",''); print_r ($_ COOKIE)

The result is that the entire $_ COOKIE array is empty, not just $_ COOKIE ['testcookie']. So I grabbed the bag with winsock, observed the returned http header, and found that the http header turned out to be "Set-Cookie: testcookie=deleted; expires=Mon, 18-Jun-2007 02:42:33 GMT", which means "setcookie (" testcookie ",'');" indeed, the cookie of testcookie is deleted directly, and this situation is not explained at all in the php manual.

Finally read the php source code, and finally found the truth (this is the advantage of open source, if there is any unclear inside information, check the source code directly).

The following code can be found near line 99 of ext/standard/head.c in php5.20 's linux source package:

The code is as follows:

If (value & & value_len = 0) {/ * * MSIE doesn't delete a cookie when you set it to a null value * so in order to force cookies to be deleted, even on MSIE, we * pick an expiry date 1 year and 1 second in the past * / time_t t = time (NULL)-31536001 Dt = php_format_date ("D, d-M-Y H:i:s T", sizeof ("D, d-M-Y H:i:s T")-1, t, 0 TSRMLS_CC); sprintf (cookie, "Set-Cookie:% slotted deleted; expires=%s", name, dt); efree (dt);} else {sprintf (cookie, "Set-Cookie:% slots% s", name, value? Encoded_value: ""); if (expires > 0) {strcat (cookie, "; expires="); 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); strcat (cookie, dt); efree (dt);}}

The source code clearly shows "if (value & & value_len = = 0)". When "value_len" is 0, "sprintf (cookie," Set-Cookie:% sliced deleted; expires=%s ", name, dt);" will send the http header to delete cookie to the browser.

Finally, we can conclude that using "setcookie ($cookiename,');" or "setcookie ($cookiename, NULL);" in php removes cookie, but it's not in these manuals.

Is it very simple? sometimes it is very necessary for us to read the php source code.

Thank you for your reading, the above is the content of "php setcookie how to delete cookie", after the study of this article, I believe you have a deeper understanding of how to delete cookie in php setcookie, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report