How to clear php cookies: First create a PHP sample file; then create cookies through setcookie; finally clear the created cookies through the "setcookie('test','',time()-3600);" method That’s it.
Recommended: "PHP Video Tutorial"
The operating environment of this tutorial: Windows 7 system, PHP version 5.6, this method is applicable For all brands of computers.
PHP clears COOKIE, PHP cannot delete COOKIE?
Set COOKIE validity period, COOKIE expiration
Mentioned in the PHP manual:
PHP transparently supports HTTP cookies. A cookie is a mechanism that stores data on a remote browser to track and identify users. Cookies can be set using the setcookie() or setrawcookie() functions. Cookies are part of the HTTP headers, so the setcookie() function must be called before other information is output to the browser, similar to the restrictions on the header() function.
setcookie(): bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, bool secure]]]]] )
To delete a cookie, you need to ensure that its expiration date is in the past to trigger the browser's deletion mechanism.
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.
For example:
setcookie('test','true',time()+3600); //创建cookie setcookie('test','',time()-3600); //清除建立的cookie
====================================== ===============================================
If you directly setcookie("test", '');
print_r($_COOKIE);
The result is that the entire $_COOKIE array is empty, not just $_COOKIE['testcookie']. So use winsock to capture Package, observe the returned http header, and find that the http header is actually
Set-Cookie: testcookie=deleted; expires=Mon, 18-Jun-2007 02:42:33 GMT
This shows that setcookie("testcookie", ''); indeed deletes the testcookie cookie directly. Regarding this situation, in php There is no explanation at all in the manual.
Finally read the php source code and finally discovered the truth (this is the benefit of open source, if there is any unclear inside story, directly check the source code)
The following code can be used in php5. Found near line 99 of ext/standard/head.c in the 20 Linux source code package.
if (value && value_len == 0) { 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: %s=deleted; expires=%s", name, dt); efree(dt); } else { sprintf(cookie, "Set-Cookie: %s=%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); } }
It is clearly displayed in the source code, if (value && value_len == 0), when value_len is 0
sprintf(cookie, "Set-Cookie: %s=deleted; expires=%s", name, dt);
will send the http header to delete the cookie to the browser.
Finally we can conclude that using
setcookie($cookiename, '');或者 setcookie($cookiename, NULL);
in php will delete the cookie , of course it is not in these manuals.
============================================== =========================================
php cookie cannot Delete/purge expired?
Today I used cookies to log in to the website. After debugging, I used
setcookie("username", "username", time()+1000,"/php100/");
to store the user’s login information, and then used
setcookie("username", "", time()-3600);
to log out. When tested under IE, it worked. any problem. Since you are building a website, you must be compatible with as many browsers as possible, haha. So I tested it in Firefox and everything worked fine when logging in. But when I launched it, I ran into trouble. There is no way to log out, the user is always logged in. So I checked the difference between cookie records in IE and Firefox, and after testing, I suddenly realized it.
It turns out that if the fourth parameter (legal path parameter) of setcookie() is not specified, the current directory will be used as the legal path by default, and the path I tested is: http://127.0.0.1/php/ rss2fla/data /log.php, so the cookie paths set when logging in and logging out are different.
IE is more user-friendly than Firefox. Haha, when specifying the path, it will overwrite the cookie variable with the same name under the current IP. However, FireFox is more strict, resulting in a new variable...
The above is the detailed content of How to clear php cookies. For more information, please follow other related articles on the PHP Chinese website!