Codeigniter - get all users session data

Codeigniter - get all users session data

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

Stefan Bogdanescu

Founder & Senior Architect

CodeIgniter Session Retrieval: Beyond $_SESSION

In native PHP, accessing session data is trivial; you simply interact with the superglobal $_SESSION. However, CodeIgniter abstracts this mechanism behind a custom session library and a database driver in many configurations. This abstraction layer means that trying to blindly access $_SESSION inside a Controller or Model will often fail or return an empty array, as the framework manages the session lifecycle differently by default.

To retrieve all session data within the CodeIgniter context, you must interact directly with the Session Facade. The most robust approach is using the session() helper function provided by the framework. This helper returns a Session instance, giving you programmatic access to the entire session storage without relying on superglobals that might be filtered out depending on your server configuration or CI version settings.

Here is how you can retrieve all stored data:

use CodeIgniter\Session\Session;

// Inside a Controller or Service class
$data = Session::get(''); 

// Returns an array of all session variables

The Session::get() method accepts an optional key. When passed an empty string, it returns the entire contents of the session as an associative array. This is functionally equivalent to iterating over $_SESSION in vanilla PHP, but it respects the encryption and storage drivers configured in your app/Config/Session.php.

You might need this capability for administrative tasks, such as bulk cleanup, analytics processing, or debugging stateful issues where you need to inspect every variable currently active for a specific user. However, retrieving all session data carries inherent trade-offs that should be considered before implementation.

Security and Performance Implications

Accessing the full session store is generally discouraged in production environments for several reasons. Sessions are meant to be opaque stores; exposing their entire contents can leak sensitive information if not handled carefully. If you iterate over every key returned by Session::get(''), you risk accidentally logging or displaying protected data like passwords, tokens, or internal flags that were stored without strict naming conventions.

Furthermore, session data is often encrypted and serialized based on your configuration (e.g., using the files or database driver). While CodeIgniter's Session Facade handles decryption transparently when reading, dumping the raw data for analysis requires ensuring you are not inadvertently exposing this during debugging. Always verify that sensitive keys are prefixed or named specifically so they can be identified and excluded from bulk operations if necessary.

Alternative: Database Access

If your application is configured to use a database driver for sessions (which is common in larger applications), you might consider accessing the raw session table directly via a Query Builder instance. This approach bypasses the Session Facade entirely but offers more granular control over what data is retrieved.

// Accessing the raw session table if using DB driver
$sessionModel = new \App\Models\Session_model(); // Ensure your model name matches Config
// Or use the default database connection
$db = service('db');
$query = $db->table(config('Session').'_sessions')
    ->select('*')
    ->get();

$data = $query->getResultArray();

This method is powerful but requires careful configuration. You must ensure that session.save_path and the database connection are correctly set up in your environment variables, as hardcoded paths or missing database credentials will cause this code to fail silently or throw exceptions depending on CI version error handling. Additionally, modifying or deleting rows directly should be done with extreme caution, as sessions are often tied to specific IP addresses and user agents that must remain consistent.

Conclusion

For most use cases requiring session inspection within CodeIgniter, the Session::get('') method provides the cleanest integration with your existing configuration. It respects encryption settings and ensures you are reading the decrypted, usable data rather than raw serialized strings. If you find yourself needing to manipulate session storage at scale or access it outside the standard controller flow, ensure that your security policies allow for such broad access before proceeding.

Our network