【问题标题】:PHP cookies not being deleted/unsetPHP cookie 没有被删除/取消设置
【发布时间】:2017-11-12 18:09:29
【问题描述】:

您好,我在远程服务器上开发了一个 Web,但出现了问题 [我取消了 cookie,从该文件中我可以看到 cookie 未设置,但在索引中我看到它们再次设置]。

我的网站具有下一个结构(我使用 MVC) 我会尽可能明确

---public_html
 --models
 --views
 --controllers
 --media
 --js
 --css
 --index.php
 --.htaccess
 --other  

.htaccess 设置为:www.eg.eg/?view=lorem --> www.eg.eg/lorem

index 在设置参数时调用views 例如:www.eg.eg/some some.php 从视图中调用

解决问题的步骤:

  1. 登录www.eg.eg/login 如果cookies 可用设置cookie 名为token
  2. 注销www.eg.eg/other/log_out.php
  3. 检查 cookie 是否未设置但未设置

要设置 cookie,我会这样做:

 setcookie("sessionPAD", $token, time() + (86400*30), "/", "www.eg.eg", true);

当我在索引中写 var_dump($_COOKIE) 时,我可以看到 sessionPAD

我要退出:

<?php
    session_start();
    if(isset($_SESSION['x']) && isset($_SESSION['y'])){
        $x= $_SESSION['x'];
        $y= $_SESSION['y'];
        require_once '../models/Connection.php';
        require_once '../models/User.php';
        $User = new User();
        $User->deleteToken($selector .  $validator);
    }
    session_destroy();
    if (isset($_COOKIE['sessionPAD'])) { //to try to delet cookie
        unset($_COOKIE['sessionPAD']);
        setcookie("sessionPAD", null, -1, "../");
        setcookie("sessionPAD", null, -1, "/");
        setcookie("sessionPAD", null, -1);
    }
    var_dump($_COOKIE); //When I see the result of this sessionPAD is not shown, so I guess session was deleted or can't be seen from that file
    //header("location:../"); //line commented to test

注销后我去index.php,猜猜看,相同值的cookie仍然存在。

我已经看到删除所有 cookie 的代码,但我只是想具体删除那个 cookie。

非常感谢你们的时间

【问题讨论】:

    标签: php cookies


    【解决方案1】:

    我找到了解决方案

    大家好,经过大量时间尝试不同的选项后,我发现了如何删除该 cookie

    setcookie($name, '', -1, "/", "www.eg.eg", true);
    

    我的解释:

    当我使用 SSL 时,我将 cookie 设置为在安全连接中可用,并且也仅在我的域中可用,因此要取消设置 cookie,我只需要指定相同的参数、域和安全连接。

    毕竟不是文件夹的事,而是参数的事

    【讨论】:

      猜你喜欢
      • 2010-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 2012-09-11
      • 2016-02-14
      • 1970-01-01
      • 2013-07-02
      相关资源
      最近更新 更多