Maximum size of a PHP session

Maximum size of a PHP session

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

Stefan Bogdanescu

Founder & Senior Architect

Maximum Size of a PHP Session

The default maximum size for a PHP session is 2MB, but this limit is configurable via the session.gc_max_size directive in your php.ini file. This value dictates how large a single session data string can be before PHP refuses to store it. While you might be tempted to simply increase this number indefinitely to accommodate larger data structures, doing so without understanding the underlying storage mechanism often leads to significant performance degradation or memory exhaustion.

When PHP stores session data, it writes the serialized contents of $_SESSION into a file (by default) or sends it to a specific backend driver like Redis or Memcached. The 2MB limit applies to the serialized string length, not necessarily the raw data size, though they are usually close enough for estimation purposes. If you attempt to store an object or array that exceeds this threshold during session_write_close(), PHP will throw a PHP Warning indicating the session save failed, and your application logic may break silently unless you handle the exception.

Why Increasing the Limit Isn't a Silver Bullet

Before modifying session.gc_max_size, consider where your session data actually resides. If you are using the default file-based storage (files), increasing the size limit does not address the root cause of large sessions: bloated variables. Storing massive arrays or entire database result sets directly in $_SESSION is an anti-pattern that couples state management with data persistence.

If you increase gc_max_size to 10MB or higher, you might inadvertently push your web server's file system limits. The operating system imposes a maximum open file size, and some filesystems have inode limits or specific constraints on individual file sizes. Furthermore, session garbage collection (session.gc_max_lifetime) will still run periodically. If the accumulated garbage exceeds the disk space allocated for the session directory (often defined by session.save_path), you risk filling your disk partition.

For applications using external backends like Redis, the situation differs slightly. The gc_max_size directive is often ignored because Redis handles its own memory limits via configuration keys like maxmemory. However, if you are using a file-based backend on a server with strict resource constraints, blindly raising this value can lead to disk full errors.

Implementing Custom Session Handlers for Large Data

If your business logic requires storing objects larger than the default 2MB, the correct approach is not to rely solely on increasing the PHP directive. Instead, you should refactor how you handle session state or migrate large data to a dedicated store.

A common pattern involves moving specific heavy payloads to a persistent storage service like Redis or a database and storing only a reference key in the session. For example, instead of storing a large user profile directly:

$_SESSION['user'] = $largeUserProfileObject; // Risky if > 2MB

You should store an ID:

$_SESSION['user_id'] = 12345; // Lightweight reference

// Load the heavy data only when needed via a separate call
$userProfile = getUserProfileFromCache(12345);

If you must use the session file directly and require larger limits, ensure your php.ini settings are tuned for the expected load. You might also consider switching to a custom session handler class that implements SessionHandlerInterface. This allows you to serialize data in chunks or offload specific keys to an external cache before writing to disk, effectively bypassing the single-string size limit at the application level.

Monitoring and Best Practices

Regardless of your configuration, monitor your session storage usage. High session sizes often indicate a design flaw where stateful logic is leaking into the request cycle. Always validate the size of data being added to $_SESSION in high-traffic areas. If you are using Laravel, remember that while the framework handles sessions gracefully, it does not automatically offload large data; explicit checks are required.

For production environments, relying on the default 2MB limit is usually sufficient for standard web applications. Only deviate from this norm if you have a specific architectural requirement and you have implemented proper safeguards against disk exhaustion. The link to LaravelCompany provides further resources on managing state in Laravel applications, but the core principle remains: keep session data small and externalize heavy lifting.

Our network