0

Why isn't the session variable value available on the second page load when I click the "Fake Login" link?

class Fake_login extends CI_Controller {

    function __construct() {
        parent::__construct();

        echo $this->session->userdata('session_id') . '<br>';
        // First Page Load: ee3c7c5da6e465605cd57ad699aacdb3
        // Second Page Load: c82adf312f123d56e3b7b6ab5ec6cafa

        echo $this->session->userdata('variable') . '<br>';
        // First Page Load: false
        // Second Page Load: false

    }

    function index($user_id = null){

        $this->session->sess_destroy();

        $this->load->library('session');
        $this->session->set_userdata('variable', 'approved');

        echo $this->session->userdata('session_id') . '<br>'; // ee3c7c5da6e465605cd57ad699aacdb3
        echo $this->session->userdata('variable') . '<br>'; // approved

        echo '<a href="/admin/fake_login/start/' . $user_id . '">Fake Login</a>';

    }

    function start($user_id = null) {
        echo 'here';
    }

}

Please note, the session in library in codeigniter is set to autoload.

This is my Session config:

$config['sess_cookie_name'] = 'cisession';
$config['sess_expiration'] = 172800; // 24 hours
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'cisessions';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 172800;
Abs
  • 56,052
  • 101
  • 275
  • 409
  • Please add more examples or scenarios. Im not really understanding what you are asking for. – CodeGodie Jun 16 '15 at 15:01
  • if you are already autoloading the session why do you have this line: `$this->load->library('session');` ? – CodeGodie Jun 16 '15 at 15:08
  • 3
    Instead of destroying the session, use `$this->session->sess_create();` to create a brand new session, which should clear ALL the data. – Craig Jun 16 '15 at 15:23
  • what Codeigniter version are you using? and are you setting your `sess_driver` to `database`? It would be helpful if you include the session information in `config.php` to your OP. – CodeGodie Jun 16 '15 at 15:32
  • I am using Codeigniter 2. I included my session config file. – Abs Jun 16 '15 at 15:40
  • @Craig that worked perfectly! I destroyed the session and simply created a new one and it worked fine! – Abs Jun 16 '15 at 16:52
  • 1
    @Abs You're welcome. I will post it as an answer. :) – Craig Jun 17 '15 at 07:28

2 Answers2

1

Instead of simply destroying the session, use;

$this->session->sess_create();

This will create a brand new session, destroying any previous sessions in the meantime.

I think this is a bug in CI2, which was fixed in version 3.

Craig
  • 1,823
  • 1
  • 11
  • 12
0
function __construct() {
        parent::__construct();

        echo $this->session->userdata('session_id') . '<br>';
        // First Page Load: ee3c7c5da6e465605cd57ad699aacdb3
        // Second Page Load: c82adf312f123d56e3b7b6ab5ec6cafa

        echo $this->session->userdata('variable') . '<br>';
        // First Page Load: false
        // Second Page Load: false

    }

and in

function index($user_id = null){

       $this->session->sess_create();//add this

        $this->load->library('session');
        $this->session->set_userdata('variable', 'approved');

        echo $this->session->userdata('session_id') . '<br>'; // ee3c7c5da6e465605cd57ad699aacdb3
        echo $this->session->userdata('variable') . '<br>'; // approved

        echo '<a href="/admin/fake_login/start/' . $user_id . '">Fake Login</a>';

    }

read this

  1. Stack Overflow Question 01
  2. Stack Overflow Question 02
Community
  • 1
  • 1
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85