CodeIgniter: store more information into the session

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.

Answers


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.


Need Your Help

Finding processes using ALSA sound fast

linux shell audio posix alsa

Currently the way /usr/sbin/alsa in Debian knows the processes using the sound card looks like:

How to assign JUnit test resolution in Jenkins/Sonar?

java testing junit jenkins sonarqube

We are a team who develop a software written in Java. We use JUnit for unit tests. We would like to find a plugin for Jenkins/Sonar to monitor the results of unit tests.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.