【问题标题】:Sessions and cookies, if I destroy a session will the cookie go away? (PHP)会话和 cookie,如果我销毁会话,cookie 会消失吗? (PHP)
【发布时间】:2012-08-04 16:34:59
【问题描述】:

我使用 PHP Session 变量让用户登录并编辑他们的网站。当他们注销时,我调用session_destroy(); 函数来终止会话。

在登录屏幕上,我可以选择“记住这台计算机”,而不是使用 $_SESSION[] 变量,我设置了一个这样的 cookie:

setcookie("admin", true, $expire);

cookie 已设置,但当我注销时,cookie 未设置。有什么办法可以防止这种情况?我希望会话结束,但我希望网站也能记住计算机。

感谢您的宝贵时间。

编辑:这是我用来启动和销毁会话的两种方法(我调用的会话方法在另一个类上)

 public function loginCheck() {

    //check if the remember me is set

    if (isset($_POST['remember'])) {
        $expire = time() + 60 * 60 * 24 * 30;
        setcookie("admin", true, $expire);
    }
    else{
        setcookie("admin", "", time()-3600);
    }


    $sth = $this->db->prepare("SELECT uid FROM philllwareusers WHERE 
            usr = :login AND pasw = :password");

    $sth->execute(array(
        ':login' => $_POST['user'],
        ':password' => $_POST['pswrd']
    ));

    $data = $sth->fetch();

    $count = $sth->rowCount();

    if ($count > 0) {

        //check if user has permision to edit this website

        $sth = $this->db->prepare("SELECT web_id FROM website_spine WHERE 
            admin_id = :uid ");
        $sth->execute(array(
            ':uid' => $data['uid']
        ));

        $datas = $sth->fetch();

        $counts = $sth->rowCount();
        if ($counts > 0) {

            if ($datas['web_id'] == WEB_ID) {



                Session::init();
                Session::set('uid', $data['uid']);
                Session::set('loggedIn', true);
                header('location: ../index');
            } else {
                header('location: ../Adminlogisn');
            }
        }

        // login
    } else {
        header('location: ../Adminlogin');
    }
}

function logout() {
    Session::init();
    Session::destroy();
    header('location: '.URL.'/Adminlogin');
}

这就是管理员登录的样子(检查 cookie 是否应该设置,应该保持设置或销毁它的部分。),

 <?php
        if(!isset($_COOKIE['admin']) || $_COOKIE['admin'] == ''){?>
    Remember this computer as an admin computer <div style="display: inline;cursor: pointer;" onclick="alert('By selecting this option, your website will remember this computer and present the login option on the menu bar when you are not logged in.')">[?]</div>
    <input type="checkbox" name="remember" style="width: 20px;"/> 
    <?php

    }
    else{
        ?>
          Un-check  to forget this computer as an admin computer. <div style="display: inline;cursor: pointer;" onclick="alert('This computer is currently rememberd as and admin computer, making the login link on the menu bar visible for easy access. Uncheck this box to forget this computer as admin computer.')">[?]</div>
    <input type="checkbox" name="remember" checked="checked" style="width: 20px;"/>
    <?php
    }
    ?>

【问题讨论】:

  • 单独设置cookie不会消失。您似乎在已经销毁的会话中检查 cookie 的值。
  • 如果用户访问该页面时未提交remember,则您取消设置cookies。也许这会导致 cookie 丢失。
  • 如果 cookie 已经存在,则会自动设置记住,用户可以通过取消选中来删除 cookie,因此会丢失页面。非常抱歉之前没有包括在内,请查看我修改后的问题。
  • 当你调用函数loginCheck(),如果$_POST['remember']没有设置,你会清除cookies。这意味着如果您在未提交表单时调用此函数,cookie 将被清除。

标签: php session cookies destroy


【解决方案1】:

无需检查您的代码。

您必须检查用户是否有 cookie(不是会话!)。如果他使用 cookie 进入网站但没有会话,则您在后台登录(并设置会话)。

如果您有适当的过期时间,则不应取消设置 cookie。但是,会话 cookie 会在浏览器关闭后消失。

【讨论】:

    【解决方案2】:

    这里是您可以找到问题答案的链接:

    http://www.codeflask.com/2012/08/why-session-destroy-when-remove-my.html

    而实际答案(引用自链接)是:

    当 cookie 被删除时,会话会自动销毁,原因是每当创建会话时,它也会将其 id 存储在 cookie 中,这就是会话自动销毁的原因。

    【讨论】:

    • 此链接指向一个单个句子,您可以在此处输入您的答案,或者如果不是您的答案,则进行总结。
    猜你喜欢
    • 2020-04-27
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 2014-07-18
    • 1970-01-01
    相关资源
    最近更新 更多