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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:59:34  来源:igfitidea点击:

how to delete all cookies of my website in php

phpcookies

提问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

PHP setcookie()

PHP setcookie()

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, '/');
    }
}

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

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

回答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:

它没:

  1. Remove parent domain cookies (from a.b.c; remove b.c; cookies),
  2. Remove cookies from a higher path other then root.
  1. 删除父域 cookie(来自 abc;删除 bc;cookie),
  2. 从除 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数据,否则保留会话数据。

回答by Borjovsky

I know this question is old, but this is a much easier alternative:

我知道这个问题很老,但这是一个更简单的选择:

header_remove();

But be careful! It will erase ALL headers, including Cookies, Session, etc., as explained in the docs.

不过要小心!它将删除所有标头,包括 Cookie、会话等,如文档所述