使用会话在Codeigniter中传递用户的详细信息

时间:2017-07-10 07:54:41

标签: php codeigniter

大家好我有一个用户控制器和User_model模型。我希望能够在用户登录后从数据库检索并显示登录用户的电子邮件和电话号码到视图。任何想法如何我可以理解这将是赞赏,如果可以编写代码来证明我会非常高兴。

MODEL

public function login($username, $password){
        //validation
        $this->db->select('id, email, username');
    $this->db->where('username', $username);
    $this->db->where('password', $password);
    $this->db->where('status', 1);
    $result = $this->db->get('users');
    if($result->num_rows() == 1){
        return $result->row(0)->id;
    } else {
        return FALSE;
    }
    }

控制器:

 public function login(){
    $data['title'] = 'Login';

    $this->form_validation-> set_rules('username', 'Username', 'required');
    $this->form_validation-> set_rules('password', 'Password', 'required');

    if($this->form_validation->run() === FALSE){

    $this->load->view('templates/header');
    $this->load->view('users/login', $data);
    $this->load->view('templates/footer');
    } else {

        // fetching user
    $username = $this->input->post('username');
        //Encrypted password
    $password = md5($this->input->post('password'));

    //login user
    $user_id = $this->user_model->login($username, $password);

    if($user_id){
        //creating session
        $user_data = array(
            'user_id' => $user_id,
            'username' => $username,
            'logged_in' => TRUE,
        );

   $this->session->set_userdata('user_data',$user_data);            
                // Set message to be sent
    $this->session->set_flashdata('user_login', 'Welcome');
    redirect('posts');
    } else {



        // Set message to be sent
    $this->session->set_flashdata('login_fail', 'Login Failed');
    redirect('users/login'); 
    }

    }

}

0 个答案:

没有答案