在我创建了一个注册表单,其中包含以下字段:user,email,pass,cpass,我在会话中保存了用户字段,因此当我提交表单时,它应该将我重定向到显示消息的主页:{{1 }}。
在我的登录表单中,我只有以下字段:电子邮件,传递,我想在提交表单后重定向以及显示相同消息的主页,但我不知道如何在会话中保存用户名。如何我能这样做吗?
答案 0 :(得分:1)
假设您在模型claass中有一个用于检查用户凭据的函数。比如 -
function check_user($email, $pass) {
//select db column that you will need for future task
$this->db->select('id, user, email, pass);
$this->db->from('user_table');
$this->db->where('email', $email);
$this->db->where('pass', $pass);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}
在你的控制器函数返回后,只需将它们放入session.like -
public function do_login(){
$result = $this->login_model->check_user($email, $pass);
//put them in the session
foreach($result as $row){
$this->session->set_userdata('user_id', $row->id);
$this->session->set_userdata('user', $row->user);
$this->session->set_userdata('email', $row->email);
//put more information as your need
}
}
请根据您的情况修改代码。