I'm thinking about adding some handling to the settings.php code, to set the ini_set('session.gc_maxlifetime', 2880); value based on the role of the logged it (or not logged in) user.

i.e. for admin role, moderator role, I'd set a real long value so as to basically not ever get logged out due to idling too long, while writing a long article, or whatever, while for "lesser" roles, like a regular member, they'll get a regular, shorter maxlifetime.

My 2 questions:

1) Is the Global $user var available within settings.php, so I can check it for the user's roles?

2) How often does it get called? Only once during initial site load? Or every time a user loads a page? I don't want to be (re)setting the value every time a page loads.


The settings.php file is included on the first bootstrap phase (DRUPAL_BOOTSTRAP_CONFIGURATION), while the global $user is initialized on DRUPAL_BOOTSTRAP_PAGE_CACHE or DRUPAL_BOOTSTRAP_SESSION, which are both later bootstrap phases.

The settings.php file is included each times Drupal bootstraps, which means every time Drupal serves a page, including the ones served from its cache. As such, the settings.php file can use plain PHP functions or a restrict set of Drupal functions; in both the cases, the code cannot access the user variable. It is not even possible to initialize it using the database, since the database connection is initialized after the file has been included. (The obvious reason is that the database settings are contained in the settings.php file.)


  • Excellent answer, thanks. That's all exactly like I had assumed, but wanted to confirm first. I've been working in WordPress for awhile now, so I'm rusty going back to Drupal. Any suggestions for doing what I want to do, without using roles, since $user isn't available at that point in time? – J. Scott Elblein Sep 26 '15 at 6:47
  • I cannot think of a way for doing that, right now. You seem to need to know the roles a user have before the session is open, but roles are available right after the session is already created. – kiamlaluno Sep 27 '15 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.