views:

195

answers:

1

this is the function I have, in my login controller, which stores data into an array and then set the userdata

  function validate_credentials()
   {
   $this->load->model('membership_model');
   $query = $this->membership_model->validate();

   if($query):
    $data = array(
     'username' => $this->input->post('username'),
     //add usertype
     //add email
     //add deposit money
     'is_logged_in' => true
    );
    $this->session->set_userdata($data);
    //should redirect to last view
    redirect('home/index');
   else:
    $this->index();
   endif;
  }// end of validate_credentials()

I would like to store more information like the usertype, email, depost,etc... the data is stored into the database. How do I get the data from the model to controller? or is this not recommended?

for example with the usertype I want to check in the views if the user is an admin and display certain options for admins only.

A: 

You can use the normal CI session handling done in CI.

$this->session->set_userdata('usertype', 'admin');

For checking for usertype. I usually have a authentication class that checks if the user logged in is an admin. e.g.

function logged_in($role = NULL)
{
    if ( ! $this->CI->session->userdata('logged_in'))
    {
        return FALSE;
    }

    if ($this->CI->user->has_role($role, $this->CI->session->userdata('user_id')))
    {
        return TRUE;
    }
    return FALSE;
} // function restrict()

About saving information, using the session, it is probably not the safest. I usually recommend checking for user information on the fly rather than storing everything in the CI session.

Thorpe Obazee
on the fly? you mean query the database each time and not store it into a session. But the sessions are saver, if you still could let me know how to store more info into the sessions.
krike
query the database each time so that the a user whose usertype has been changed while he is logged in could work as soon as he loads the page.
Thorpe Obazee
Anyway, to store information you only need to use $this->session->set_userdata('usertype', 'admin');
Thorpe Obazee
You could then read it via $this->session->userdata('usertype');
Thorpe Obazee