Abs
Abs

Reputation: 57916

Setting session variable after destroying previous session

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;

Upvotes: 0

Views: 88

Answers (2)

Craig
Craig

Reputation: 1823

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.

Upvotes: 1

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

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

Upvotes: 0

Related Questions