How can I set session expiration time dynamically in codeigniter?
For example, if a user logs in and has the role of admin
, the expiration time should be longer than if a user logs in who does not have an admin
role.
Thanks.
How can I set session expiration time dynamically in codeigniter?
For example, if a user logs in and has the role of admin
, the expiration time should be longer than if a user logs in who does not have an admin
role.
Thanks.
None of these solutions address doing this dynamically or require another variable to be added to the session. The solution I came up with for CI 3.0.4 is to extend Session.php.
Create file
application/libraries/Session/MY_Session.php
Put the following into the file and modify for your logic of setting the
$expiration
variable. In my case I am pulling the value from a database. NOTE: If you have different expiration values per user type; there is a chance they could get garbage collected and expire unexpectedly due to different expirations with the same session. In this case I do NOT recommend this approach.use something like this:
At codeigniter go to applications/config.php and find the below configuration.
You can solve the session issue by replacing this:
with this:
You can handle this with a custom controller. When a user logs in, set a session variable with the time of login. Create custom controller that contains a function in the constructor to check if the user is not admin user and if the timeout has expired. If it has, call $this->session->destroy(); Now, make all your controllers extend that controller instead of the CI base controller.