如何在codeigniter中使用$ this-> session-> set_userdata

时间:2017-09-29 12:30:08

标签: php codeigniter session continuous-integration

当我使用$ this-> session-> set_userdata

时,我对CI有一些问题

控制器

 public function check_login()
{
    $email    = $this->input->post('username_email');
    $password = $this->input->post('passwords');
    $password_encrypt = sha1(md5($password));

    $query = $this->model->check_login($email,$password_encrypt);
    if( $query->num_rows() > 0 )
    {
        $row = $query->row(1);
        $data = array(
          'email'           => $row->email,
          'level_id'        => $row->level_id,
          'id_subunit'      => $row->id_subunit,
        );
        $this->session->set_userdata($data);
        return TRUE;
    }
    else{
        return FALSE;
    }
}

模型

function check_login($email,$password_encrypt)
{
    $db = $this->load->database('default',TRUE);
    $where_email = array('email' => $email, 'password' => $password_encrypt);

    $db->where($where_email);
    $query = $db->get('admin',1);

    return $query;
}

我试着进入这里,获取会话数据

public function home()
{
    $data['level'] = $this->session->userdata('level_id');
    $data['email'] = $this->session->userdata('email');
    $data['id_subunit'] = $this->session->userdata('id_subunit');

    if($data['level'] == 1){
        redirect('admin');
    }
    else if($data['level'] == 2){
        redirect('kuesioner/submited'); 
    }
    /*else{
         redirect('sub_unit/home');
    }*/
}

我的问题是我只能从会话中的数组中获取电子邮件和级别ID

如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

在控制器或config / autoload.php文件中加载会话库。

$this->load->library('session');

然后设置会话数据。

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

答案 1 :(得分:0)

也许逗号

$data = array(
  'email'           => $row->email,
  'level_id'        => $row->level_id,
  'id_subunit'      => $row->id_subunit
);