如何通过codeigniter中的会话访问所有userdata

时间:2016-06-06 19:30:55

标签: php codeigniter

我已经创建了一个登录名并传递了电子邮件和ID,我可以在我的视图中访问它们。但我无法访问其他用户数据,如姓名,电话等。我只能访问电子邮件,身份证和会话ID。感谢您的帮助。

这是我的模特

public function login($email, $password){
    $this->db->select('*');
    $this->db->from($this->table);
    $this->db->where('email', $email);
    $this->db->where('passwd', $password);
    $this->db->limit(1);

    $query = $this->db->get();

    if ($query->num_rows() == 1) {
        return $query->row()->uid;
    }
    else {
        return FALSE;
    }

}

这是我的控制器

public function login(){

    $this->form_validation->set_rules('email', 'Email', 'trim|required');
    $this->form_validation->set_rules('password', 'Password', 'trim|required');


if($this->form_validation->run() == FALSE) {

    echo validation_errors('<p class="alert alert-dismissable alert-danger">');
}
else{

        //Get post data
        $email = $this->input->post('email');
        $password = $this->input->post('password');

        $enc_pwd = sha1($password);
        $id = $this->User_model->login($email, $enc_pwd);

        if($id){
            $userdata = array(
                'id'        =>  $id,
                'email'     =>  $email,
                'logged_in' =>  TRUE
                );


            //Set session data
            $this->session->set_userdata($userdata);


            //Redirect to Users page
            redirect('site/dashboard');
        }
        else{
            //Create Error
            $this->session->set_flashdata('error', 'Login credentials are incorrect');


            //Redirect to Users page
            redirect('site/login');
        }

        //Add activity
        //$this->Activity_model->add($data);            
    }

    //Load the login template
    $this->load->view('public/login');

}

1 个答案:

答案 0 :(得分:3)

您可以在模型

中设置您的sesssion

参考下面的代码。检查如何在会话中设置其他变量

<强>模型

function login($email, $password)
{
    $this -> db -> select('*');
    $this -> db -> from($this->table);
    $this->db->where('email', $email);
    $this->db->where('passwd', $password);
    $this->db->limit(1);
    $query = $this -> db -> get();  
    if($query -> num_rows() == 1)
    {
        $row = $query->row();
        $data = array(
        'email'=>  $email,
        'id' => $row->id,
        'name' => $row->name,
        'phone' => $row->phone,
        'logged_in' =>  TRUE 
        );
        $this->session->set_userdata($data);
        return true;

    }
    else
    {
        return false;
    }
  }

您可以调用该会话变量的任何地方。