Codeigniter:自动登录网站上新注册的用户并重定向到个人资料页面

时间:2015-12-08 15:06:22

标签: php codeigniter autologin

我正在使用Codeigniter注册和登录表单。我创建它们都在工作。现在我需要在网站上成功注册后自动登录用户。

添加用户代码:

 public function add_user() {
    $data = array(
        'username' => $this->input->post('user_name'),
        'email' => $this->input->post('email_address'),
        'password' => md5($this->input->post('password'))
    );
    $this->db->insert('user', $data);
}



注册和验证:

public function registration() {
    $this->load->library('form_validation');
    // field name, error message, validation rules
    $this->form_validation->set_rules('user_name', 'User Name', 'trim|required|min_length[4]|xss_clean');
    $this->form_validation->set_rules('email_address', 'Your Email', 'trim|required|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
    $this->form_validation->set_rules('con_password', 'Password Confirmation', 'trim|required|matches[password]');

    if ($this->form_validation->run() == FALSE) {
        $this->index();
    } else {
        $this->user_model->add_user();
        $this->thank();
    }
}

1 个答案:

答案 0 :(得分:1)

在其他方面,

else {
    $id = $this->user_model->add_user();
    // set session here like how you will set on login
    $data["user_id"] = $id; 
    ...// other required data for session
    $this->session->set_userdata($data);
    $this->thank();
}

在模型中,在插入数据后返回最后一个插入ID

$this->db->insert('user', $data);
return $this->db->insert_id();//   <--- this line