doujiu9307 2019-04-20 18:22
浏览 85
已采纳

在codeigniter登录后无法保存用户的所有数据

I am developing a login method in my project. I can not keep all my user data in the session after login. I can only keep the data which I used for my login.

Like I can keep email and type in my session But I can not keep the name of the user in the session.

My controller

public function login()
{
    $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[3]|alpha_dash');
    $this->form_validation->set_rules('type', 'Type', 'trim|required');

    if($this->form_validation->run() == FALSE)
    {
        /*=== LOAD DYNAMIC CATAGORY ===*/
        $this->load->model('admin_model');
        $view['category'] = $this->admin_model->get_category();
        /*==============================*/

        $view['user_view'] = "users/login";
        $this->load->view('layouts/user_layout', $view);
    }
    else
    {
        $this->load->model('user_model');

        $email = $this->input->post('email');
        $password = $this->input->post('password');
        $type = $this->input->post('type');

        $user_data = $this->user_model->login_user($email, $password, $type);

        if($user_data)
        {
            $login_data = array(

                'user_data' => $user_data,
                'email'     => $email,
                'type'      => $type,
                'name'      => $name,
                'logged_in' => true

            );

            $this->session->set_userdata($login_data);

My model

public function login_user($email, $password, $type)
{
    $this->db->where('email', $email);
    $this->db->where('type', $type);

    $result = $this->db->get('users');

    $db_password = $result->row('password');

    if(password_verify($password, $db_password))
    {
        return $result->row(0)->id;
    }
    else
    {
        return false;
    }
}

I want to keep all data of a user into the session after login. How I can do that?

  • 写回答

1条回答 默认 最新

  • doufu6196 2019-04-20 21:18
    关注

    You are close to having what you want. The biggest problem is with this line

    return $result->row(0)->id;
    

    Because that returns only the value of the 'id' column. Fix that (and make the code more compact) like this

    public function login_user($email, $password, $type)
    {
        $result = $this->db
            ->where(array('email' => $email, 'type' => $type))
            ->get('users')
            ->row(0); //$result is the whole row - all the columns
    
        if($result && password_verify($password, $result->password))
        {
            return $result;
        }
    
        return false;
    }
    

    Then the important part of the controller makes use of getting the whole row with this.

    $user_data = $this->user_model->login_user($email, $password, $type);
    
    if($user_data)
    {
        $login_data = array(
            'user_data' => $user_data,  //all the columns from row(0)
            'email' => $user_data->email, //email column from row(0)
            'type' => $type,
            'name' => $user_data->name, // name column from row(0)
            'logged_in' => true
        );
    
        $this->session->set_userdata($login_data);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序