You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: _posts/2022-03-30-drupal-tips-changing-session-lifetime-for-users.md
+18-5
Original file line number
Diff line number
Diff line change
@@ -70,7 +70,7 @@ session.gc_maxlifetime = 3600
70
70
session.gc_probability = 1
71
71
session.gc_divisor = 1000
72
72
```
73
-
But in the Drupal context, we can change these values from a Drupal installation, inside the file `services.yml` in `docroot/sites/default/`:
73
+
But in the Drupal context, we can change these values from a Drupal installation, inside the file `default.services.yml` in `web/sites/default/` or in `core.services.yml` in `web/core/`:
74
74
75
75
```
76
76
parameters:
@@ -80,7 +80,7 @@ parameters:
80
80
gc_maxlifetime: 200000
81
81
cookie_lifetime: 2000000
82
82
```
83
-
As you can see expanded in the original block:
83
+
As you can see expanded in the original block: from `default.services.yml`:
84
84
```
85
85
session.storage.options:
86
86
# Default ini options for sessions.
@@ -134,17 +134,30 @@ gc_maxlifetime: 3600 (only one hour of lifetime for the session).
134
134
cookie_lifetime: 0 (deleted when user close the browser tab).
135
135
```
136
136
137
-
## Overwriting default values by switching service class
137
+
## Overwriting default values by switching service class
138
138
139
+
**The bad news** is that the above values are statically part of the service container after a Drupal installation, and cannot be modified "dynamically". And these previous values are in the by-default configuration of the Drupal installation.
139
140
141
+
**The good news** is that we can add others values by switching with another class to replace the service provision. . Besides this is part of the Container in Drupal, so can only be overwritten from a custom Drupal module using a pair of classes. We can do something like:
142
+
143
+
-`gc_maxlifetime: 3600` (only one hour of lifetime for the session, and then make the session available for the garbage collector).
144
+
-`cookie_lifetime: 0 ` (deleted when user closes the browser tab).
145
+
146
+
Only we need change the class responsible for the sessions management, used in service `session_configuration` as described in `core.services.yml`:
147
+
148
+
```
149
+
session_configuration:
150
+
class: Drupal\Core\Session\SessionConfiguration
151
+
arguments: ['%session.storage.options%']
152
+
```
140
153
141
154
From a ServiceProvider class, by linking the service to our new custom class:
142
155
143
156
```
144
157
public function alter(ContainerBuilder $container) {
0 commit comments