繁体   English   中英

会话和cookie,如果我破坏了一个会话,cookie会消失吗? (PHP)

[英]Sessions and cookies, if I destroy a session will the cookie go away? (PHP)

我使用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进入网站但没有会话,则可以在后台登录他(并设置会话)。

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

在此链接中,您可以找到问题的答案:

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

实际答案(从链接中引用)为:

删除Cookie时,会话会自动销毁,原因是无论何时创建会话,它都会将其ID也存储在cookie中,这就是会话自动销毁的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM