Codeigniter - 会话数据不会被设置

时间:2018-03-31 23:34:33

标签: php codeigniter session web-development-server

我在设置会话数据时遇到问题。我想将会话数据设置为布尔值' loggedIn'和一个id' userID'。出于某种原因,在提交登录表单时,login()函数将到达重定向到dashboard()函数的行,但随后在仪表板功能处停止。

控制器

    public function login() {
//  echo 'login page';
        $this->session->set_userdata('userID', NULL);
        $this->session->set_userdata('loggedIn', NULL);

        $data['session_data'] = array(
            'userID' => $this->session->userdata('userID'),
            'loggedIn' => $this->session->userdata('loggedIn')
        );

        $this->load->view('navigation');
        $this->load->view('login', $data);

        if ($this->session->userdata('loggedIn') == TRUE) {
            $this->session->set_flashdata('error_msg', 'please log out to access this page ');
            echo 'Please log out to access this page!...';
            sleep(2);
            redirect('index.php/user/dashboard');
        }

        if ($this->input->post('login')) {
            $this->form_validation->set_rules('username', 'username', 'required');
            $this->form_validation->set_rules('password', 'password', 'required');

            $login_details = array(
                'username' => $this->input->post('username'),
                'password' => $this->input->post('password')
            );

            if ($this->form_validation->run() == true) {
                $this->session->set_flashdata('sucess_msg', 'form running ');
                $verify_password = $this->user_model->verify_password($login_details);

                if ($verify_password == true) {
                    $this->session->set_flashdata('error_msg', 'password verified true ');
                    $userID = $this->user_model->get_userID($login_details);

                    $data_session = array(
                        'loggedIn' => TRUE,
                        'userID' => $userID
                    );

                    $session_loggedIn = array('loggedIn' => TRUE);
                    $session_userID = array('userID' => $userID);
                    $this->session->set_userdata('loggedIn', $session_loggedIn);
                    $this->session->set_userdata('userID', $session_userID);

                    //$this->session->set_userdata('loggedIn', $data_session['loggedIn']);
                    //$this->session->set_userdata('userID', $data_session['userID']);
                    $this->session->set_flashdata('success_msg', 'loggedIn and userID changed to current log in account ');

                    echo 'USER ID: ' . $this->session->userdata('userID');
                    //$this->load->view('dashboard', $);
                    redirect('index.php/user/dashboard');
                } else {
                    $this->session->set_flashdata('error_msg', 'wrong email or password, try again!...');
                    //redirect('index.php/user/login');
                }
            }
        }
    }

    public function dashboard() {

        if ($this->session->userdata('loggedIn') == FALSE) {
            $this->session->set_flashdata('error_msg', 'please log in to access the dashboard page');
            sleep(2);
            redirect('index.php/user/login');
        } else {
            $data['user'] = array(
                'user' => $this->user_model->get_user_data($this->session->userdata('userID'))
            );

            $this->load->view('navigation');
            $this->load->view('dashboard', $data);
        }
    }

模型

public function verify_password($login_details){
  $this->db->select('password');
  $this->db->from('user_account');
  $this->db->where('username', $login_details['username']);
  $query = $this->db->get->result_array();

  if($query[0] == $login_details['password']){
    return true;
  } else {
    return false;
  }

}

配置

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = APPPATH.'cache/sessions/';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = TRUE;

结果

session data: Array ( [userID] => [loggedIn] => ) 
session data: Array ( [__ci_last_regenerate] => 1522538883 [userID] => [loggedIn] => )

程序将达到仪表板功能的第一个条件,然后重定向回登录页面,因为它表示会话数据已记录在'未设置为TRUE。

0 个答案:

没有答案