I used custom session driver with using config/session_custom.php file, sometime it is working and sometime not

in .env it's like below

SESSION_CUSTOM_DRIVER=redis
SESSION_CUSTOM_LIFETIME=180
SESSION_CUSTOM_COOKIE="${APP_NAME}_custom_session"

and i used when like below when storing data into session

config(['session' => include config_path('session_custom.php)]);
Session::put('custom_cart_data', $data);

so sometimes it works and sometimes it get expired before 180 minutes

1

There are 1 best solutions below

1
Jaydeep Khokhar On BEST ANSWER

The behavior you're experiencing with sessions expiring before their set lifetime can be attributed to several factors. When using a custom session driver, especially with Redis, and configuring it dynamically within your application, there are a few key points and potential issues to consider:

  1. Dynamic Configuration Reliability: Dynamically changing the session configuration using config(['session' => include config_path('session_custom.php')]); might not always be reliable, especially if it's done conditionally or not early enough in the application lifecycle. The session driver and its settings need to be configured before the session starts. If this configuration is applied after the session has already been initiated, it may not have the intended effect.

  2. Session Lifetime Configuration: The session lifetime is set in minutes, and you've set it to 180. However, the actual expiration might also depend on the session driver's (Redis, in your case) configuration and how it handles expiration and eviction policies. For example, Redis might evict keys early if it's running low on memory, depending on its maxmemory-policy.

  3. Cookie Expiration: The session cookie's lifetime is also a factor. Make sure the session cookie's expiration matches the desired session lifetime. Inconsistencies here might lead to sessions appearing to expire early if the cookie is no longer valid.

  4. Application Environment: Ensure that the .env file changes are reflected in the environment. Sometimes, cached configurations can cause old values to persist. Running php artisan config:cache can refresh this, but remember to do it only in production, as it might interfere with .env changes being picked up in a development environment.

  5. Redis Configuration: Ensure that Redis is configured correctly for your use case. The maxmemory setting and eviction policy in Redis can affect how long session data is retained. If Redis decides to evict data early due to memory pressure, your sessions will end prematurely.

To address these issues:

  • Early Configuration: Apply your session configuration as early as possible in your application's lifecycle. Ideally, this configuration should be done in a service provider's boot method or even before Laravel's session handling starts.

  • Session Driver Configuration: Ensure that your session driver (Redis) is correctly configured in session_custom.php and that Laravel is indeed using this configuration. Debugging or logging the configuration being used can help verify this.

  • Investigate Redis Behavior: Check your Redis instance's configuration, especially around eviction policies and memory limits, to ensure it aligns with your session data retention needs.

  • Persistent Configuration: Instead of dynamically configuring the session driver, consider setting it up statically in the configuration files or through environment variables. This approach is more reliable and predictable.

  • Session Debugging: Add logging to key parts of your application to track session creation, modification, and expiration. This can help identify when and why sessions are expiring or being recreated.

If you continue to experience issues, you may want to consider using Laravel's built-in session debugging tools or a package that can provide more insight into session handling. Additionally, ensure that your application logic that switches the session configuration does so under predictable and repeatable conditions to avoid unintentional session loss.