【问题标题】:Codeigniter - Get other database information and place into SessionCodeigniter - 获取其他数据库信息并放入 Session
【发布时间】:2017-01-05 06:04:41
【问题描述】:

我是 CodeIgniter 的新手,我创建了一个登录系统,其中我有一个会话。在这里,我可以通过使用 POST 获取帐户的用户名并将其放入会话数组中。但是,我还想从数据库中获取它的其他数据,例如它的名字、姓氏等,并将其也放入会话中。为了让我实现它,正确的语法是什么?

控制器:

function validate_login()
{
    $this->load->model('model_accounts');
    $valid = $this->model_accounts->validate();
    $isAdmin = $this->model_accounts->check_role();
    $isActive = $this->model_accounts->check_active();

    if($valid && $isAdmin && $isActive) // Active Admin
    {
        $data = array(
            'username' => $this->input->post('username'), 
            'password' => $this->input->post('password'),
            'is_logged_in' => true
        );

        $this->session->set_userdata($data);
        redirect('admin_ticketing');
    }
    else if(($valid && $isActive) && $isAdmin == false)  // Active User
    {
        $data = array(
            'username' => $this->input->post('username'),
            'password' => $this->input->post('password'),   
            'is_logged_in' => true
        );

        $this->session->set_userdata($data);
        redirect('user_home');
    }
    else if(($valid && $isAdmin) && $isActive == false)  //Deactivated Admin
    {
        $data = array(
            'username' => $this->input->post('username'),
            'password' => $this->input->post('password'),
            'is_logged_in' => true
        );

        $this->session->set_userdata($data);
        redirect('login/admindeact');
    }
    else if($valid && ($isActive && $isAdmin) == false) //Deactivated User
    {
        $data = array(
            'username' => $this->input->post('username'),
            'password' => $this->input->post('password'),
            'is_logged_in' => true
        );

        $this->session->set_userdata($data);
        redirect('login/userdeact');
    }
    else if($valid == false) //Invalid Account
    {
        $data['main_content'] = 'view_login';
        $data['message'] = "The username and password you entered did not match our records. Please double-check and try again. ";
        $this->load->view('includes/login_template', $data);
    }
}

型号:

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

    if($query->num_rows() == 1)
    {
        return true;    
    }
    else
    {
        return false;
    }
}

function check_role() 
{
    $this->db->where('username', $this->input->post('username'));
    $this->db->where('password', $this->input->post('password'));
    $this->db->where('role', 1);
    $query = $this->db->get('accounts');

    if($query->num_rows() == 1)
    {

        return true;    
    }
    else 
    {
        return false;
    }
}

function check_active() 
{
    $this->db->where('username', $this->input->post('username'));
    $this->db->where('password', $this->input->post('password'));
    $this->db->where('isActive', 1);
    $query = $this->db->get('accounts');

    if($query->num_rows() == 1)
    {
        return true;    
    }
    else 
    {
        return false;
    }
}

【问题讨论】:

  • 您可以在会话中创建一个数组并存储您需要的所有数据,如下所示 $this->session->set_userdata('sessionarr',$resultset);
  • 我已经创建了这个数组$data = array( 'username' => $this->input->post('username'), 'password' => $this->input->post('password'), 'is_logged_in' => true ); 但我想从数据库中获取上面用户名的名字。我想知道怎么做
  • 使用提供的用户名和密码从数据库中获取用户数据,并像您说的那样再次存储...eg: if($query->num_rows() == 1) { return $query->result; //it contains all userdata }
  • @coderszx 检查以下答案。

标签: php codeigniter session


【解决方案1】:

从控制器完成页面重定向并在模型上设置会话如下:

控制器:

function validate_login()
{
    $this->load->model('model_accounts');
    $valid = $this->model_accounts->validate();
    $isAdmin = $this->model_accounts->check_role();
    $isActive = $this->model_accounts->check_active();

    if($valid && $isAdmin && $isActive) // Active Admin
    {
        redirect('admin_ticketing');
    }
    else if(($valid && $isActive) && $isAdmin == false)  // Active User
    {

        redirect('user_home');
    }
    else if(($valid && $isAdmin) && $isActive == false)  //Deactivated Admin
    {

        redirect('login/admindeact');
    }
    else if($valid && ($isActive && $isAdmin) == false) //Deactivated User
    {

        redirect('login/userdeact');
    }
    else if($valid == false) //Invalid Account
    {
        $data['main_content'] = 'view_login';
        $data['message'] = "The username and password you entered did not match our records. Please double-check and try again. ";
        $this->load->view('includes/login_template', $data);
    }
}

型号:

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

    if($query->num_rows() == 1)
    {
     $data = array(
            'username' => $result->username, 
            'password' => $result->password,
            'is_logged_in' => true
        );
        $this->session->set_userdata($data);
        return true;    
    }
    else
    {
        return false;
    }
}

function check_role() 
{
    $this->db->where('username', $this->input->post('username'));
    $this->db->where('password', $this->input->post('password'));
    $this->db->where('role', 1);
    $query = $this->db->get('accounts');
    $result = $query->row();

    if($query->num_rows() == 1)
    {
         $data = array(
            'username' => $result->username, 
            'password' => $result->password,
            'is_logged_in' => true
        );
        $this->session->set_userdata($data);
        return true;    
    }
    else 
    {
        return false;
    }
}

function check_active() 
{
    $this->db->where('username', $this->input->post('username'));
    $this->db->where('password', $this->input->post('password'));
    $this->db->where('isActive', 1);
    $query = $this->db->get('accounts');
    $result = $query->row();

    if($query->num_rows() == 1)
    {
        $data = array(
            'username' => $result->username, 
            'password' => $result->password,
            'is_logged_in' => true
        );
        $this->session->set_userdata($data);
        return true;    
    }
    else 
    {
        return false;
    }
}

【讨论】:

  • 嗨!我收到一个错误,即:致命错误:调用未定义的方法 stdClass::num_rows() 并指向此处:` if($query->num_rows() == 1)`
  • 别忘了加载session()库。
猜你喜欢
  • 2015-06-02
  • 2012-10-31
  • 2016-01-23
  • 2014-12-07
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
  • 2023-03-26
  • 2023-03-04
相关资源
最近更新 更多