CodeIgniter - Why can't I access a session set from a controller in another controller?

Viewed 703

This is Thisclass controller where I originally set a session variable where $data is an array containing values fetched from the database

<?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    /**
    * 
    */
    class Thisclass extends CI_Controller
    {
        function __construct()
        {
            parent::__construct();
        }

        function index()
        {
            // The code for fetching values is here
            $this->session->set_userdata("mysession",$data);
        }
    }//end class
?>

And this is the Anotherclass controller where I am trying to access that variable

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* 
*/
class Anotherclass extends CI_Controller
{   
    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        var_dump($this->session->userdata('mysession'));
    }
}//end class

However, that var_dump returns null. I already checked the session library and it is included in the autoload.php. And I checked the index again and again and it maches. So, what might be the reason/s why I can't call that variable ? In the web server, it actually works. The session variable can be accessed in another controller and in this local copy, it doesn't seem to work. I already checked the necessary settings and it seems fine. Thanks you for your help..

1 Answers

For this specific situation, this solution worked.

I changed session.auto_start=0 to session.auto_start=1 in the php.ini of xampp. I found an answer here: php - Session are lost after redirect in CodeIgniter 3

I realized that this is an issue about the web server configuration.

Related