Troubleshooting Session Timeout Misconfigurations in Apache
When configuring PHP sessions to persist for a specific duration, it is easy to assume that setting variables in php.ini is sufficient. You might change session.gc_maxlifetime = 1440 and session.cache_expire = 1500, expecting the session data to remain valid for exactly 12 hours (720 seconds). However, if your application reports shorter lifetimes or loses state unexpectedly, the issue usually lies in how these values interact with other system settings or how the garbage collector actually functions.
The session.gc_maxlifetime directive defines the maximum lifetime of a session file before the PHP garbage collector considers it eligible for deletion. Setting this to 1440 seconds (24 minutes) will not extend the session beyond that window unless other conditions are met. If you observe sessions timing out after only a few hours despite setting session.gc_maxlifetime = 1440, the most common culprit is the time-to-live header sent by the web server itself, not the PHP configuration.
Apache manages session cookies through its own timeout mechanism, which is independent of PHP's internal logic. When you set session.cookie_httponly or rely on standard browser behavior, the browser will respect the Max-Age attribute found in the Set-Cookie header. If Apache is configured with a directive that overrides this, such as SessionTimeout in certain modules or if the PHP session handler interacts with a different cookie storage backend (like Redis), you may see discrepancies.
To achieve a true 12-hour session duration, you must ensure that the Set-Cookie header explicitly defines a Max-Age of 43200 seconds. You can force this behavior by adding a custom directive in your .htaccess file or Apache virtual host configuration:
<IfModule mod_php7.c>
# Force session cookie to live for 12 hours
PHPSessionCookieLifetime 43200
</IfModule>
However, note that PHPSessionCookieLifetime is not a standard directive in all Apache versions. In most production environments, you rely on the PHP session handler to output the correct header. If your application uses a custom session handler or stores data in Redis/Memcached, the session.gc_maxlifetime value becomes less critical for file-based expiration but still matters for logic-based timeouts.
Another frequent pitfall is the interaction between session.cache_expire and the actual garbage collection frequency. The session.cache_expire directive controls how long to keep session files in the cache directory, but it does not override gc_maxlifetime. If your application runs under a load that triggers the garbage collector frequently (e.g., every 5 minutes), sessions may be deleted even if they are less than 24 minutes old. The garbage collector is probabilistic; it checks whether the current time exceeds session.gc_maxlifetime + session.gc_probability * session.gc_divisor. If your server has low traffic but high CPU load, the GC might run more often than expected.
If you are using Laravel or a similar framework, you should verify that the framework's default configuration does not override these values. For example, Laravel's config/session.php file might set lifetime to 120 minutes by default, which would conflict with your Apache/PHP settings unless explicitly overridden in the application code.
To debug this effectively, check the actual cookie headers being sent:
curl -I http://your-domain.com | grep -i "set-cookie"
Look for a Max-Age attribute. If it is missing or set to a low value, your session will expire sooner than expected regardless of your php.ini settings.
For persistent sessions across multiple servers or in distributed environments, consider using an external store like Redis or Memcached with the PHP session handler. This removes reliance on file system timeouts and gives you full control over expiration logic through your application code:
session_set_save_handler(new SessionHandlerRedis(), true);
ini_set('session.gc_maxlifetime', 43200);
By aligning your Apache, PHP, and application-level configurations, you can ensure that sessions truly last the intended 12 hours without unexpected interruptions.