简体   繁体   中英

How to access all userdata via session in codeigniter

Please I have created a login and passed email and id and I can access them in my view. But I am unable to access other userdata like name, phone, etc. Am only able to access email, id and session id. Your help is appreciated.

here is my model

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;
    }

}

here is my controller

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');

}

You can set your sesssion in the Model

Refer the code below.Check how to set other variables in session

Model

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;
    }
  }

Anywhere you can call that session variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM