【问题标题】:PHP - Can't end sessionPHP - 无法结束会话
【发布时间】:2017-07-18 01:08:46
【问题描述】:

登录后我的网站上有一个链接,指向“index.php?logout=yes”,然后在我的 index.php 中有以下代码:

if ($_GET['logout'] == "yes") {
    session_unset();
    unset($_SESSION);
    session_destroy();
    echo "<meta http-equiv='refresh' content='0;url=index.php'>";
    $_SESSION = array();
    $_SESSION = [];
}

任何想法为什么它让您保持登录并且不结束会话?

【问题讨论】:

  • 手动刷新页面是否有效?
  • 不,因为我有一个语句来检查会话是否已设置,如果是,则将您重定向到 login.php。
  • 是否应该在任何其他检查之前进行注销声明?
  • 您必须先检查,index.php 上的条件是否为真?logout=yes ?
  • session_unset(); == unset($_SESSION);$_SESSION = array(); == $_SESSION = [];

标签: php session logout unset


【解决方案1】:

如果是的话,这是你的 index.php 中的第一行代码吗? header('位置:/'); 而不是用于刷新的元标记

【讨论】:

    【解决方案2】:

    注销后,将用户重定向到主页(例如)

    并且总是在使用之前启动会话

    <php 
    session_start();   
    if ($_GET['logout'] == "yes") {
       session_unset();
       session_destroy();
       session_write_close();
       setcookie(session_name(),'',0,'/');
       session_regenerate_id(true);
       header('location: index.php');
    }
    

    【讨论】:

      【解决方案3】:

      请查看:session_destroy

      为了完全终止会话,例如注销用户,还必须取消设置会话 ID。如果使用 cookie 传播会话 id(默认行为),则必须删除会话 cookie。 setcookie() 可以用于此。

      试试这个

      session_unset(); //or unset($_SESSION);
      session_destroy();
      session_commit(); // or session_write_close();
      setcookie(session_name(),'',0,'/');
      session_regenerate_id(true);
      
      header('location: index.php');
      

      【讨论】:

        【解决方案4】:

        公共函数注销() {

            $this->session->set_userdata(array(
                'user_id'       => '',
                'fname'         => '',
                'lname'         => '',
                'gender'        => '',
                'username'      => '',
                'user_comp_id'  => '',
                'user_role'     => '',
                'validated'     => false,
                'plan_name'     => '',
                'plan_restriction' => '',
                'module_leave_bank' => '',
                'module_custom_logo' => '',
                'module_bulk_import' => '',
                'module_custom_rules' => '',
                'module_reports_download' => '',
                'module_integrations' => '',
                'module_chat_support' => '',
                'module_phone_support' => '',
                'users_limit'       => '',
                'leave_policies_limit' => '',
                'ous_limit'         => '',
                'sub_ous_limit'     => '',
                'leave_requests_limit' => '',
                'holidays_limit'    => '',
                'users_count'       => '',
                'leave_policies_count' => '',
                'ous_count'         => '',                       
            ));
            $cookies_id = $this->input->cookie('ci_session', TRUE);
            // $this->db->delete('user_logged_in',array('cookie' => $cookies_id)); // delete the cookies in database
            $this->session->sess_destroy();
        
            redirect('login', 'refresh');
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-11-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多