2

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..

Vincent
  • 53
  • 7
  • There could be a possibility that this may be a difference in settings of the server and localhost and if so, what settings should I have to check ? – Vincent May 22 '20 at 03:47
  • Don’t forget to add session library in autoload application/config/autoload.php $autoload['libraries'] = array('session'); OR load in controller __contruct() function $this->load->library('session'); – Rahul Gupta May 22 '20 at 04:04
  • where is $data that you are passing? $this->session->set_userdata("mysession",$data); – Nadeem Ijaz May 22 '20 at 05:46

1 Answers1

0

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.

Vincent
  • 53
  • 7