开发者

CodeIgniter: store more information into the session

开发者 https://www.devze.com 2022-12-21 05:22 出处:网络
this is the function I have, in my login controller, which stores data into an array and then set the userdata

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 opt开发者_JAVA技巧ions for admins only.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号