简体   繁体   English

CodeIgniter:将更多信息存储到会话中

[英]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 这是我在登录控制器中拥有的功能,该功能将数据存储到数组中,然后设置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. 我想存储更多信息,例如用户类型,电子邮件,depost等,这些数据存储在数据库中。 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. 例如,对于用户类型,我想在视图中检查用户是否是管理员,并仅显示管理员的某些选项。

You can use the normal CI session handling done in CI. 您可以使用在CI中完成的常规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. 我通常有一个身份验证类,用于检查登录用户是否为管理员。 eg 例如

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. 我通常建议即时检查用户信息,而不是将所有内容存储在CI会话中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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