我正在致力於Codeigniter 3身份驗證應用程序。我是新來的CI,所以我遇到了這個基本問題:無法存儲/輸出會話中的變量(我需要他顯示登錄的用戶數據)。Codeigniter 3身份驗證:無法從會話中回顯變量
的用戶模式:
class Usermodel extends CI_Model {
public function user_login($email, $password)
{
$query = $this->db->get_where('users', array('email' => $email, 'password' => $password));
return $query->row();
}
}
的簽到控制器看起來是這樣的:
class Signin extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('signin');
}
public function signin()
{
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
if ($this->form_validation->run())
{
$email = $this->input->post('email');
$password = $this->input->post('password');
$this->load->model('Usermodel');
$current_user = $this->Usermodel->user_login($email, $password);
if ($current_user) {
$current_user_id = $this->session->set_userdata('user_id', $current_user->id);
$current_user_email = $this->session->set_userdata('email', $current_user->email);
// Echo from database
echo $current_user->id . " | " . $current_user->email;
//Echo from session
echo $current_user_id . " | " . $current_user_email;
} else {
redirect('signin');
}
}
else
{
$this->load->view('signin');
}
}
}
我能在控制器,打印出用戶ID簽名和數據庫中的電子郵件地址爲echo $current_user->id . " | " . $current_user->email;
。瀏覽器顯示:1 | [email protected]
。
但我不能在會議上做同樣的事情:echo $current_user_id . " | " . $current_user_email;
什麼都不輸出。
我的錯誤在哪裏?謝謝!
這裏解釋獲取會話數據https://www.codeigniter.com/user_guide/libraries/sessions.html#retrieving-session-data並設置https://www.codeigniter.com/user_guide/libraries/sessions.html #adding-session-data – user4419336