php 如何在php中删除我网站的所有cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2310558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to delete all cookies of my website in php
提问by Mac Taylor
I'm wondering if I can delete all my website's cookies when a user click on logout, because I used this as function to delete cookies but it isn't work properly:
我想知道当用户单击注销时是否可以删除我网站的所有 cookie,因为我使用它作为删除 cookie 的功能,但它无法正常工作:
setcookie("user",false);
Is there a way to delete one domain's cookies in PHP?
有没有办法在 PHP 中删除一个域的 cookie?
回答by jasonbar
Taken from that page, this will unset all of the cookies for your domain:
从该页面获取,这将取消设置您域的所有 cookie:
// unset cookies
if (isset($_SERVER['HTTP_COOKIE'])) {
$cookies = explode(';', $_SERVER['HTTP_COOKIE']);
foreach($cookies as $cookie) {
$parts = explode('=', $cookie);
$name = trim($parts[0]);
setcookie($name, '', time()-1000);
setcookie($name, '', time()-1000, '/');
}
}
回答by poke
$past = time() - 3600;
foreach ( $_COOKIE as $key => $value )
{
setcookie( $key, $value, $past, '/' );
}
Even better is however to remember (or store it somewhere) which cookies are set with your application on a domain and delete all those directly.
That way you can be sure to delete all values correctly.
然而,更好的是记住(或将其存储在某个地方)您的应用程序在域上设置了哪些 cookie,并直接删除所有这些 cookie。
这样您就可以确保正确删除所有值。
回答by Doug
I agree with some of the above answers. I would just recommend replacing "time()-1000" with "1". A value of "1" means January 1st, 1970, which ensures expiration 100%. Therefore:
我同意上面的一些答案。我只建议用“1”替换“time()-1000”。值“1”表示 1970 年 1 月 1 日,可确保 100% 到期。所以:
setcookie($name, '', 1);
setcookie($name, '', 1, '/');
回答by knittl
make sure you call your setcookie function before any output happens on your site.
确保在您的网站上发生任何输出之前调用您的 setcookie 函数。
also, if your users are logging out, you should also delete/invalidate their session variables.
此外,如果您的用户正在注销,您还应该删除/使他们的会话变量无效。
回答by Roman Holzner
When you change the name of your Cookies, you may also want to delete all Cookies but preserve one:
当您更改 Cookie 的名称时,您可能还想删除所有 Cookie 但保留一个:
if (isset($_COOKIE)) {
foreach($_COOKIE as $name => $value) {
if ($name != "preservecookie") // Name of the cookie you want to preserve
{
setcookie($name, '', 1); // Better use 1 to avoid time problems, like timezones
setcookie($name, '', 1, '/');
}
}
}
Also based on this PHP-Answer
也基于这个PHP-Answer
回答by Wesley Abbenhuis
The provided Answers did not solve my problem,
提供的答案没有解决我的问题,
It did not:
它没:
- Remove parent domain cookies (from a.b.c; remove b.c; cookies),
- Remove cookies from a higher path other then root.
- 删除父域 cookie(来自 abc;删除 bc;cookie),
- 从除 root 之外的更高路径中删除 cookie。
My script does, see.
我的脚本可以,看。
<?php function unset_cookie($name)
{
$host = $_SERVER['HTTP_HOST'];
$domain = explode(':', $host)[0];
$uri = $_SERVER['REQUEST_URI'];
$uri = rtrim(explode('?', $uri)[0], '/');
if ($uri && !filter_var('file://' . $uri, FILTER_VALIDATE_URL)) {
throw new Exception('invalid uri: ' . $uri);
}
$parts = explode('/', $uri);
$cookiePath = '';
foreach ($parts as $part) {
$cookiePath = '/'.ltrim($cookiePath.'/'.$part, '//');
setcookie($name, '', 1, $cookiePath);
$_domain = $domain;
do {
setcookie($name, '', 1, $cookiePath, $_domain);
} while (strpos($_domain, '.') !== false && $_domain = substr($_domain, 1 + strpos($_domain, '.')));
}
}
It is not the most pretty/safe/optimal solution, so use this only if you do not known the cookie-path and/or cookie-domain's. Or use the idea in order to create your version.
它不是最漂亮/安全/最佳的解决方案,因此仅当您不知道 cookie 路径和/或 cookie 域时才使用它。或者使用这个想法来创建你的版本。
回答by Gajus
All previous answers have overlooked that the setcookiecould have been used with an explicit domain. Furthermore, the cookie might have been set on a higher subdomain, e.g. if you were on a foo.bar.tar.comdomain, there might be a cookie set on tar.com. Therefore, you want to unset cookies for all domains that might have dropped the cookie:
之前的所有答案都忽略了setcookie可以与显式域一起使用。此外,cookie 可能已设置在更高的子域中,例如,如果您在foo.bar.tar.com域中,则可能在 上设置了 cookie tar.com。因此,您希望为可能已删除 cookie 的所有域取消设置 cookie:
$host = explode('.', $_SERVER['HTTP_HOST']);
while ($host) {
$domain = '.' . implode('.', $host);
foreach ($_COOKIE as $name => $value) {
setcookie($name, '', 1, '/', $domain);
}
array_shift($host);
}
回答by Martin LeBlanc
You should be aware of various tracking tools like Google Analytics also use cookies on your domain and you don't want to delete them, if you want to have correct data in GA.
您应该知道 Google Analytics 等各种跟踪工具也会在您的域中使用 cookie,如果您想在 GA 中获得正确的数据,则您不想删除它们。
The only solution I could get working was to set the existing cookies to null. I couldn't delete the cookies from the client.
我可以开始工作的唯一解决方案是将现有的 cookie 设置为 null。我无法从客户端删除 cookie。
So for logging a user out I use the following:
因此,为了注销用户,我使用以下命令:
setcookie("username", null, time()+$this->seconds, "/", $this->domain, 0);
setcookie("password", null, time()+$this->seconds, "/", $this->domain, 0);
Of course this doesn't delete ALL cookies.
当然,这不会删除所有 cookie。
回答by Dan Bray
Use the function to clear cookies:
使用函数清除cookies:
function clearCookies($clearSession = false)
{
$past = time() - 3600;
if ($clearSession === false)
$sessionId = session_id();
foreach ($_COOKIE as $key => $value)
{
if ($clearSession !== false || $value !== $sessionId)
setcookie($key, $value, $past, '/');
}
}
If you pass truethen it clears sessiondata, otherwise session data is preserved.
如果通过,true则清除session数据,否则保留会话数据。

