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 运筹学排序问题中的在线排序
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧