Codeigniter session data lost after redirect

Codeigniter session data lost after redirect

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Codeigniter session data lost after redirect

In a production deployment of CodeIgniter 2.1.0, it is common to encounter a scenario where session data appears intact immediately after setting it in a controller, only to vanish once the application redirects to a new page. The specific symptom described involves a login flow: credentials are validated, user data is pushed into the session array via $this->session->set_userdata(), and a redirect to the dashboard occurs. While print_r($this->session->all_userdata()) confirms the data exists right before the redirect, accessing that same data in the destination controller yields an empty session.

This discrepancy usually points to a server-side configuration issue rather than a flaw in the CodeIgniter framework itself. The most frequent culprit is the difference between how local development environments and production servers handle session storage.

On localhost, PHP defaults to storing sessions in a file within the C:\wamp\temp or /tmp directory on Linux. These directories are often writable by default without strict permissions checks. However, when deploying to a live environment, particularly those running under Apache with mod_php or Nginx with PHP-FPM, the session save path and mode must be explicitly configured in php.ini.

If your production server uses a different user for the web server process (e.g., www-data on Ubuntu) than the one writing to the default session directory, you may encounter permission errors. When the session driver attempts to write data after the redirect logic completes, the file system denies access because the process lacks write permissions. In many cases, this error is silent; CodeIgniter catches the exception and logs it or simply fails to persist the session ID cookie update. The framework then treats the subsequent request as a new session without history, resulting in lost data upon load.

To resolve this, inspect your php.ini file on the production server. You must explicitly define the session save path if your system doesn't default to one:

session.save_path = "/var/lib/php/sessions"

Ensure that the directory /var/lib/php/sessions exists and is owned by the web server user with write permissions (typically 750 or 770). Additionally, verify the session.save_handler. If you are using a database to store sessions, which is a robust choice for high-concurrency environments, set:

session.save_handler = db
; You will also need to define the module and table name depending on your setup
session.save_path = "codeigniter_sessions"

Another critical factor in CodeIgniter 2.x is the session cookie lifetime. If session.gc_maxlifetime in php.ini is set to a very low value, or if the httponly and secure flags on the session cookie are misconfigured for your hosting environment (e.g., forcing HTTPS only when the connection is HTTP), the browser might discard the cookie prematurely. However, given that the data prints before the redirect, the issue is almost certainly persistence during the transition between requests.

Finally, check if you are running PHP versions that differ significantly between localhost and production. CodeIgniter 2.1.0 was released when PHP 5.x was standard. If your live server has been upgraded to PHP 7 or 8 without proper opcode caching or configuration tuning, legacy session handling might behave differently. Ensure that the session.auto_start directive is not inadvertently interfering with your application's initialization logic, though this is less likely in a controlled CI setup.

When troubleshooting production sessions, always compare the error logs of both environments. A silent failure to write the session file on the server side is the primary reason for the "data lost after redirect" phenomenon. By aligning the php.ini configuration and ensuring directory permissions match your deployment user, you can restore consistent session behavior across all pages in your application.

Our network