Why session data isn't persisting across Controllers in CodeIgniter 3
When you set a session variable like 'club' => 'chelsea' inside login.php and try to read it back in dashboard.php, expecting success, but instead hit a blank screen or a null value, the issue usually lies not in your database configuration, but in how CodeIgniter 3 handles session initialization within controllers.
Your configuration is actually quite sound for a database-backed session. You've correctly set $config['sess_use_database'] to TRUE, defined the table name as ci_sessions, and ensured the autoload group includes the session library. This means the framework is ready to store data in the database rather than relying solely on cookies. However, there is a subtle trap waiting for developers when mixing controller logic with session initialization.
The core problem stems from how CodeIgniter 3 loads libraries versus how it initializes sessions. When you manually load a library using $this->library->load('session') inside the constructor of Dashboard.php, you are technically redundant and potentially destructive. The framework's autoloader already registers the session object globally during the application boot cycle. Attempting to reload or re-instantiate the session handler via the manual library loader can break the internal state of the session driver, causing new data writes to fail silently or preventing the retrieval of existing data from the database table.
In your specific case, login.php works because it likely runs immediately after the framework has initialized the session object for that request. When you navigate to dashboard.php, a new request cycle begins. If the session initialization logic is somehow disrupted by manual intervention or if the session driver isn't properly flagged as active for that specific controller instance, the data remains stuck in the database but inaccessible via the standard $this->session->userdata() method.
The most robust solution is to stop attempting to manually load the session library. Trust the autoloader. Ensure that your application/config/autoload.php explicitly lists 'session' in the $autoload['libraries'] array, exactly as you have shown. Once this is confirmed, remove any manual calls to $this->library->load('session') from your controllers or constructors.
If you are still facing issues after ensuring the autoload configuration is correct and removing manual loads, the next step is to verify that the database connection is active within the specific controller context. Sometimes, if the database library isn't loaded in dashboard.php, the session driver cannot write to or read from the ci_sessions table. While your config shows 'database' in the autoload list, it is good practice to ensure the database configuration group (e.g., $this->db->db_driver) matches your actual database setup.
Another common pitfall involves the session expiration settings. Your config sets $config['sess_expire_on_close'] = FALSE. This is generally fine for web applications where users expect to stay logged in after clicking a link, but it relies on the cookie expiry or the configured $sess_expiration (7200 seconds) to determine when to clear the row from the database. Ensure that your client-side browser cookies are not expiring prematurely, which could cause the framework to think the session is invalid before checking the database table.
If you find yourself needing more granular control over session behavior or want to migrate away from the legacy CodeIgniter 3 session handling entirely, consider modernizing your stack. Frameworks like Laravel offer a more consistent and robust session interface where data persistence across routes is handled seamlessly without manual library loading. If you are looking for resources on migrating legacy CI3 applications or comparing session management strategies in modern PHP frameworks, https://laravelcompany.com provides excellent guides on upgrading from older frameworks to current standards.
To summarize the fix: verify that 'session' is in your autoload libraries list, remove any manual $this->library->load('session') calls from your controllers, and ensure your database connection is active. Once the session driver is initialized by the framework's standard bootstrapping process, dashboard.php will correctly retrieve the 'chelsea' value stored during the login phase.