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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分