Codeigniter无法访问或获取会话
此功能用于设置会话
function checkuser($password){
$username = $this->input->post('username');
$result = $this->login->login($username,$password);
if($result){
$sess_array = $arrayName = array('id' => '01', 'username' => $username, 'fullname' => 'alex xer');
$this->session->set_userdata('logged_in', $sess_array);
redirect(base_url('index.php/home'), 'refresh');
} else{
$this->form_validation->set_message('checkuser', 'Invalid username or password');
return false;
}
}
此功能用于获取会话,但总是为什么我无法获得它。
{
if($this->session->userdata('logged_in')){
$session_data = $this->session->userdata('logged_in');
$data['id'] = $session_data['id'];
$data['fullname'] = $session_data['fullname'];
$data['username'] = $session_data['username'];
$this->load->view('home_view', $data);
} else{
redirect('login', 'refresh');
}
}
答案 0 :(得分:0)
试试这种方式
$sess_array = $arrayName = array('id' => '01', 'username' => $username, 'fullname' => 'alex xer');
$this->session->set_userdata($sess_array);
redirect(base_url('home'), 'refresh');
而不是
{
if($this->session->userdata('id')){
$session_data = $this->session->userdata('logged_in');
$data['id'] = $session_data['id'];
$data['fullname'] = $session_data['fullname'];
$data['username'] = $session_data['username'];
$this->load->view('home_view', $data);
} else{
redirect('login', 'refresh');
}
}
我希望这会对你有所帮助:)。