douchigu1723 2014-10-30 12:56
浏览 45
已采纳

在用户登录时包含会话数据中的数据库条目(CodeIgniter)

What I'd like to do is on validating a CI user, when setting their session data, pull some data from a field set in the db.

At the moment I have this within my controller;

function validate()
{
    $query = $this->model_auth->validate();

        if ($query) // if the user's credentials validated 
        {
            $data = array(
                'username'      => $this->input->post('username'),
                'is_logged_in'  => TRUE
            );
            $this->session->set_userdata($data);
            redirect('dashboard/');
        } else {
            $data['error'] = 'Invalid User Id and Password combination';
            $this->load->view('view_login',$data);
        }

}

and this is my model

function validate() {
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        $query = $this->db->get('users');

        if($query->num_rows == 1) { return TRUE; } 

    }//validate

What I'd like to do is set something like this;

$data = array(
                'username'      => $this->input->post('username'),
                'user_level'    => $ThisIsSomethingWithinTheDB,
                'is_logged_in'  => TRUE
            );

Within my controller, with $ThisIsSomethingWithinTheDB relating to the relevant entry within the db under the user_level column.

I'm currently still very much learning as I go so any help would be very much appreciated.

  • 写回答

3条回答 默认 最新

  • dongtan1845 2014-10-30 14:31
    关注

    Thanks for your help both,

    I was able to achieve this as follows;

    model;

    function validate() {
            $this->db->where('username', $this->input->post('username'));
            $this->db->where('password', md5($this->input->post('password')));
            $query = $this->db->get('users');
    
            if($query->num_rows == 1)
            {
                return $query;
            }
            else
            {
                return FALSE;
            }
    
    
        }
    

    Controller;

    function validate()
    {
        $query = $this->model_auth->validate();
    
            if ($query) // if the user's credentials validated 
    
            {
            $user_data = $query->row();
            $data = array(
                'username'      => $this->input->post('username'),
                'user_level'    => $user_data->user_level,
                'is_logged_in'  => TRUE
            );
            $this->session->set_userdata($data);
            redirect('dashboard/'); 
            } else {
            $data['error'] = 'Invalid User Id and Password combination';
            $this->load->view('view_login',$data);
            }
    
    }
    

    Thanks again for your help!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?