0

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.

1 Answer 1

2

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.)

Reference

4
  • 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? Commented Sep 26, 2015 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.
    – avpaderno
    Commented Sep 27, 2015 at 10:24
  • @J.ScottElblein You could read the roles directly from the database, but that isn't a good way, especially for code you need to update for Drupal 8. (Even without accessing the database, the code would still radically change, but it won't depend on implementation details.)
    – avpaderno
    Commented Jul 31, 2022 at 9:22
  • 1
    Thanks for revisiting this. I don't recall if I ever solved this at the time because I've had to move onto other projects, but hopefully in the near future I'll be revisiting it again since I have an "ancient" Drupal 5 site than needs to be updated to the latest Drupal 8. (eek). Commented Jul 31, 2022 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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