Welcome Guest, Not a member yet? Register   Sign In
Set session expiration time based on user role in CodeIgniter 4
#3

(04-11-2025, 06:18 AM)Beewez Wrote: Hi everyone,

I'm working with CodeIgniter 4 and I need to configure the session expiration time dynamically based on the user role at login.

I understand that, by default, CodeIgniter loads session settings from app/Config/Session.php, where the 'expiration' value is defined. However, this configuration is static and applied globally.

What I want to achieve is something like this:
  • If the user has the admin role, their session should last longer.
  • If the user has a standard role, the session should expire sooner.

I’m wondering if there’s a recommended way to override the session expiration time at runtime based on the user role. Is it possible to change the expiration after login, or would I need to implement a custom logic to handle expiration manually?

Any advice or example would be greatly appreciated.
Thanks in advance!

I have a session.user variable to check if "logged in" and use the following code in a preController.php. You could change to base on user role.

Code:
session()->markAsTempdata('user', ENVIRONMENT == 'development' ? 3600 * 2 : 3600 * .5);
Reply


Messages In This Thread
RE: Set session expiration time based on user role in CodeIgniter 4 - by paulkd - 04-13-2025, 11:22 PM



Theme © iAndrew 2016 - Forum software by © MyBB