CodeIgniter Forums
My session username gets lost in redirection? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: My session username gets lost in redirection? (/showthread.php?tid=61499)



My session username gets lost in redirection? - lexxtoronto - 04-20-2015

login.php controller: I check if a user exists in db, set session data then redirect them to another page:

Code:
$sessiondata=array('username'=>$username, 'loginuser'=>TRUE);
$this->session->set_userdata($sessiondata);
redirect("mypage/view/admin");

mypage.php controller: I check if page is admin and then redirect:

Code:
public function view($page='index')
   {
       if ( ! file_exists(APPPATH.'/views/tlc/'.$page.'.php'))
        {
            // Whoops, we don't have a page for that!
            show_404();
        }
        else if ($page=='admin')
       {    
           if ($this->session->userdata('username'))
        then load admin view.

But $this->session->userdata('username') is always empty! I load session library in login.php constructor. Default session data is there i.e. I can retrieve session_id from the session cookie but not custom set session data. I tested setting custom session data on one page and then echo it it works, so setting custom session works, its just it gets lost on the way from one controller to another.

EDIT: Im also using sessions table. Is it possible that CI checks sessions cookie with session db data and because they dont match since theres custom session data CI regenerates sessions cookie? Whenever I reload the page there is a new session_id.


RE: My session username gets lost in redirection? - lexxtoronto - 04-20-2015

Hm... This is was the problem. I had $config['sess_match_useragent']=TRUE; when I changed it to FALSE my code worked. I didnt change browsers - its Opera by the way - it obviously was regenerating session cookie, so somehow CI failed to see that it was the same browser, or maybe something to do with wamp... Weird cos ideally I would like to check if its the same browser.