【问题标题】:Delete all domain cookies without knowing the path在不知道路径的情况下删除所有域 cookie
【发布时间】:2017-08-24 22:46:10
【问题描述】:

我不小心设置了一些我不应该设置的 cookie。例如,我的 cookie 名为“lang”,而我的错误是没有设置域,也没有设置 cookie 的路径。因此,浏览器使用默认值创建了 cookie,这意味着访问我网站的“产品”页面的用户最终会得到如下 cookie:

Name        Value        Domain        Path
lang        en           example.com   /products/

实际上,cookie 应该是:

Name        Value        Domain        Path
lang        en           example.com   /

错误代码存在了一段时间,这意味着 cookie 的路径恰好是用户访问的任何页面。

我的问题是,我是否可以在不知道路径的情况下删除 example.com 域下的所有 cookie?

我在JS和PHP都试过了,但是没有传递cookie的路径,浏览器拒绝删除它。考虑到坏代码的生存时间,可能有数千条路径。之所以有这么多路径是因为大部分用户来自谷歌,所以他们直接登陆像example.com/products/a-product这样的页面,这意味着cookie的路径将是/products/a-product

谢谢

【问题讨论】:

  • 我们谈论的是您无法访问代码的网站还是您可以完全访问代码的网站?
  • 我的网站,在这里我可以完全访问代码。

标签: javascript php cookies


【解决方案1】:

以下是 PHP 中可能的解决方案...我们只是通过更改过期时间来取消设置 cookie。请向documentation举报。

删除所有 cookie

if (isset($_SERVER['HTTP_COOKIE'])) {
    // We get the server cookies
    $server_cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    // We set the expiration time
    $expiration_time = time() - 3600;
    // And the default _path
    $default_path = '/';

    foreach ($server_cookies as $cookie) {
        // We explode given cookie in order to simply get the cookie name
        $all_parts = explode('=', $cookie);
        // We get the cookie name
        $name = trim($all_parts[0]);

        // We set the expiration time & empty the value
        setcookie($name, '', $expiration_time);

        // We set the path, empty the value and set the expiration time
        setcookie($name, '', $expiration_time, $default_path);
    }
}

当我们知道它的名称和信息时删除特定的 cookie

// We set the cookie name we wand to delete
$name = 'lang'; // According to OP
// We set the path of the cookie
$path = '/products/';
// We set the domain of the cookie
$domain = 'exemple.com';
// We set the expiration time
$expiration_time = time() - 3600;

// We set the expiration time & empty the value
setcookie($name, '', $expiration_time);
// We set the path, empty the value and set the expiration time
setcookie($name, '', $expiration_time, $path, $domain);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 2018-12-01
    • 1970-01-01
    • 2015-10-11
    相关资源
    最近更新 更多