get logged in user info in CodeIgniter (HMVC & Ion

2019-05-30 18:58发布

问题:

I am trying to get Logged in user details in CodeIgniter (HMVC & Ion Auth)

$data['user'] = $this->ion_auth->user()->row();

When i try to display i am getting error

Undefined variable: user

I am not sure how to get loggin in user id or email. I need id or email. Please help

回答1:

If your are trying to display it in view,

Are you passing the $data variable to the view ?

$data['user']=$this->ion_auth->user()->row();
$this->load->view("filename",$data);

If you are trying to access it with the controller you can access the variable like

$data['user']=$this->ion_auth->user()->row();
$username=$data['user']->username;

Please specify where you are initializing it and where you are accessing it.



回答2:

I will say one example. Follow this example may be it will help you.

Suppose you have a login form with Email and Password fields. When you submitted that form it will check whether you submitted properly with validation errors and everything. If everything is okay. It will check whether it is valid email and password match from database. If that also true than those email and username will save to session data in model of MVC design pattern. Once when you succeeded you just need to retrieve user information from that session data in controller and you need pass that data in an array to the next page after login.

$logged_in = $this->session->userdata("logged_in_user");