当 httponly 为 true 时,Cookies 抵抗 setcookie() 删除

Cookies resist setcookie() deletion when httponly is true

我使用 cookie 实现 'remember me' 选项以登录我的网站。设置 cookie 工作正常。取消设置曾经有效。然后我制作了一个新版本的网站,带有httponly cookies;注销在本地主机上工作,但在服务器上不起作用。我 运行 目录 "test" 中的新代码;我仍然可以使用旧代码,这 运行 适用于旧登录功能(不是新功能)中设置的 cookie。

以防万一它可能很重要,并证明我在 related questions like uncorrect parameters, testing on the same page, headers already sent 或取消设置 cookie 时的相对时间的答案中没有解决任何常见问题,我展示了我的代码。首先,设置cookies(新的)的代码:

...
setcookie ('login', $login, time()+60*60*24*30,'/','e-history.cz', false, true);
setcookie ('pass', $pass, time()+60*60*24*30,'/','e-history.cz', false, true);
...

我的注销脚本:

<?php
  include 'login_functions.php'; 
  logout(); 
?>

以及登出功能:

  function logout() { 
    include 'library.php';  //all functions and constants

    //var_dump( headers_sent() );  //I've tested the headers, not sent yet

    checkSession();  //starts session if not started
    unset($_SESSION['login']);  //I don't need to delete the rest of the session for logout

    if(isset($_COOKIE['login']))  {
      setcookie ('login', '', 1,'/','e-history.cz', false, true);
    }   
    if(isset($_COOKIE['pass']))  {
      setcookie ('pass', '', 1,'/','e-history.cz', false, true);
    }

    header('Location:index.php'); 
  }

我测试了 headers - 它们工作正常。响应 headers(根据 Chrome 的 HTTP Spy 扩展)包括以下内容:

Set-Cookie  pass=deleted;
expires=Thu, 01-Jan-1970 00:00:01 GMT;
path=/;
domain=e-history.cz;
httponly
Set-Cookie  login=deleted;
expires=Thu, 01-Jan-1970 00:00:01 GMT;
path=/;
domain=e-history.cz;
httponly

编辑 - 我将我以前的 "answer" 作为问题的一部分,因为它只工作了一段时间;我不知道为什么它现在不起作用。我在 non-httponly、httponly 和混合 cookie 之间切换了几次;每次我在创建新 cookie 之前删除旧 cookie 并注意不要使它们不匹配。无论如何,httponly 似乎并没有什么不同。

I have found a similar question saying that unsetting the httponly cookie is not possible without server interaction. Theoretically, it shouldn't be a problem, as I use php, which works server-side. However, it's not so easy in practice. But there's a solution, suggested in its answer: to make one of the cookies with and the other without httponly property. I was afraid that having the old value in the password cookie would collide with login as another user, but I tested it and it works fine, the new user's password just overwrites the old user's one.

问题不在 httponly 中,甚至不在我链接的任何其他问题中,而是在域中 - "main" 域 (e-history.cz) "domain" cookie 属性 自动以点为前缀。在测试子目录 (e-history.cz/test) 上,这没有发生。结果是我可以删除主域生成的 cookie,即使测试域使用 httponly cookie,但我无法从测试子目录中删除 cookie。我明确地将点添加到域中,现在一切正常。对主域和测试域使用相同的 cookie 现在似乎不是问题 - 如果它是一个问题,我会完全删除点(通过设置 null 或 '' 来调用默认值,而不是设置域)。