CodeIgniter 4 Shield session cookie expiration not being updated

I’m using CI4 with Shield. I want the session to stay active indefinitely. In Config\Session.php I set the “expiration” and “timeToUpdate” properties to 60 * *60 ** 24 * 365 (I also tried setting to 0). However, the cookie “ci_session” that gets created has an expiration of the same day still. How do I actually change that?

Action: Set Config\Session->expiration = 60 * *60 ** 24 * 365

Expectation: I expected the ci_session cookie to be created with an expiration date of next year.

Result: ci_session cookie is created with same day expiration still.

Understanding the Problem:

The expiration and timeToUpdate properties in Config\Session.php control the cookie’s lifespan and update frequency within the CodeIgniter session system. However, when using Shield, the session management might be overridden or extended, potentially affecting these settings.

Troubleshooting Steps:

  1. Check Shield Configuration:
  • Verify that Shield’s configuration doesn’t override the session settings. Look for any settings related to session expiration or cookie parameters in Shield’s configuration files.
  • If Shield is modifying the session behavior, you might need to adjust its configuration to allow for indefinite session persistence.
  1. Inspect Cookie Attributes:
  • Use browser developer tools to inspect the ci_session cookie’s attributes. Look for the expires attribute, which should indicate the cookie’s expiration time.
  • If the expires attribute is set to a specific date, it overrides the configuration settings.
  1. Consider Shield’s Session Handler:
  • If Shield is using a custom session handler, ensure that the handler correctly implements indefinite session persistence. Check the handler’s code for any logic that might be limiting the session’s lifespan.
  1. Test with a Basic Session:
  • Temporarily disable Shield or replace it with CodeIgniter’s default session handler to isolate the issue. If the session persists correctly without Shield, the problem likely lies in Shield’s configuration or session handler.

Possible Solutions:

  • Adjust Shield Configuration: If Shield is modifying session settings, adjust its configuration to allow for indefinite persistence. Refer to Shield’s documentation for specific instructions.
  • Modify Session Handler: If Shield uses a custom session handler, modify it to store sessions in a way that allows for indefinite expiration (e.g., using a database or file storage with appropriate expiration settings).
  • Set Cookie Attributes Directly: If Shield doesn’t provide a direct way to set cookie expiration, you might be able to set the expires attribute manually using JavaScript or browser-specific methods. However, this approach might not be ideal for security or cross-browser compatibility.

Additional Tips:

  • Debug Session Storage: Use logging or debugging tools to inspect how the session is being stored and updated. This can help identify any issues with the session handler or storage mechanism.
  • Consider Security Implications: While indefinite session persistence can be convenient, it also poses security risks. Ensure that your application has appropriate security measures in place to protect against session hijacking and other vulnerabilities.

By following these steps and carefully analyzing the behavior of your application and Shield, you should be able to resolve the issue and achieve indefinite session persistence in your CodeIgniter 4 application.