Welcome Guest, Not a member yet? Register   Sign In
My session username gets lost in redirection?
#1

(This post was last modified: 04-20-2015, 05:44 PM by lexxtoronto.)

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.
Do the one thing you think you cannot do. Fail at it. Try again. Do better the second time. The only people who never tumble are those who never mount the high wire.
Reply
#2

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.
Do the one thing you think you cannot do. Fail at it. Try again. Do better the second time. The only people who never tumble are those who never mount the high wire.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB