Codeigniter session data lost after redirect

I am using codeigniter 2.1.0.

I am trying to do a register/login function using the session library in the codeigniter.

The register/login with the session library worked fine for localhost, but when I put it live and tried it, the session does not work.

My controller login works this way. I check the credentials, once ok I set my session data and redirect to another page.

$user_data = array(
                   'username'       => $result->user_name,
                   'email'          => $result->user_email,
                   'userid'         => $result->user_id,
                   'role'           => $result->user_role,
                   'login_state'    => TRUE,
                   'lastlogin'      => time(),
               );

$this->session->set_userdata($user_data);           
print_r( $this->session->all_userdata());            
redirect(base_url('dashboard'));

at this point here when I print all my session data, they do print out. But at the dashboard controller side, when i attempt to print the session data out, they were not there anymore.

Any idea why? Thanks in advance for the help.


if you are working in CI 3.x and just upgraded your server php version to php 7.x

Go to system/libraries/Session/session.php at Line no 281 and replace ini_set('session.name', $params['cookie_name']); by ini_set('session.id', $params['cookie_name']);


I'm not sure what exactly is the problem. Recently I faced this too..

It was working before in my development running php7.0.

Currently it is only working in my production server running nginx and php 5.6. My development server seems to be not working and keeps on regenerate new row in sessions table. My development server is using php7.1, on homestead virtualbox development environment, usually being used for Laravel projects.

I managed to get over this by taking this step.

1) Go to system/libraries/Session/Session.php

2) Comment session_start() by adding //. We want to relocate the sessionn_start().

3) Go down to line 315 where it says Security is king, and comment out until line 351

enter image description here

4) Then go to your main index.php ( the root index.php )

5) Add session_start() at the top once.

enter image description here

6) Okay try again. Hopefully it works. My guess is that it is not working with php 7.1 and some update need to be done in this Session.php file.

  1. My CI Version is 3.1.1

enter image description here


PHP 7 Upgrade - * Known SESSION / COOKIE Bug

This answer addresses the known session/cookie bug - when you upgrade to PHP7 from PHP 5.

If your CodeIgniter version is @ 3.1.0 or below - and you are upgrading to PHP 7.1 - You will need to update CodeIgniter.

There is a bug with $this->session->set_userdata(); - that can be pretty annoying. It will overwrite your session as soon as you redirect or visit another page within your site structure.


Some other discussions about the bug: https://github.com/bcit-ci/CodeIgniter/issues/4830

*Save some time and see post by See post "dyanakiev commented on Oct 23, 2016" -

"Just to confirm: Everything works perfect with 3.1.1, no more problems with sessions. 👍 Good job! 💃"


See upgrade instructions here: https://www.codeigniter.com/use…/installation/upgrading.html

Latest CodeIgniter Download here: https://codeigniter.com/download

*I can also confirm this as well:

Updating codeigniter to 3.1.6 fixed the session problem immediately - that had occurred after I updated server to PHP 7.1.*