Welcome Guest, Not a member yet? Register   Sign In
Session Data lost when redirect to a view
#1

I have setting a session in my controller  by using
PHP Code:
           $this->load->model('user_m');
 
           $ui $this->user_m->login($username$password);
 
           if ($ui) {
 
               
                $this
->load->library('session');
 
               $this->session->set_userdata('userinfo',$ui); 

But when i redirect it to my view.... it gives nothing... 
1) where's the session data goes.
2)How to get the session data in my view
Reply
#2

(This post was last modified: 06-19-2017, 02:14 PM by natanfelles. Edit Reason: . )

(06-19-2017, 01:45 PM)hafizfarooq Wrote: I have setting a session in my controller  by using
PHP Code:
           $this->load->model('user_m');
 
           $ui $this->user_m->login($username$password);
 
           if ($ui) {
 
               
                $this
->load->library('session');
 
               $this->session->set_userdata('userinfo',$ui); 

But when i redirect it to my view.... it gives nothing... 
1) where's the session data goes.
2)How to get the session data in my view


Do you tried var_dump($_SESSION['userinfo']) in your view?
Reply
#3

Are you talking about an HTTP redirect, or are you just trying to access the session data in your views?

If you are talking about an HTTP redirect, you should redirect before setting the session data.

If you are just trying to access session data in your view, you can either use $this->load->vars(), or access the session data directly from the view. Another option is to use the second parameter of view(), and pass the session vars as an array.
Reply
#4

In CI, you can't redirect to a view.
A view is loaded inside a controller function.
If you redirect to another controller/function that doesn't load the session library, your session data is lost.
That's why it's good practice to autoload the session library in application/config/autoload.php once you decide to use session data.
Reply
#5

(This post was last modified: 06-20-2017, 12:36 AM by hafizfarooq.)

(06-19-2017, 10:05 PM)Wouter60 Wrote: In CI, you can't redirect to a view.
A view is loaded inside a controller function.
If you redirect to another controller/function that doesn't load the session library, your session data is lost.
That's why it's good practice to autoload the session library in application/config/autoload.php once you decide to use session data.

Thanks for the reply.. I already added 'session' in autoload library array.... but i think it is not loading bcoz when i load session library in __constructor it works.. Why it is not loading through autoload ??
Reply
#6

Please share your autoload.php file.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB