1

In my module, I need greater session control, beyond simple max_lifetime and other parameters taken from PHP config. What I did was to add a column to the sessions table that gives me the effective session expiration, which I can compare against to know if to revoke the session. (I also want to "cache" this in the local session, but this is unrelated to the question).

The question is, what is the best way to retrieve the new data from the sessions table? I can just send a direct DB query, such as:

Database::getConnection()->query('SELECT expires_at FROM {sessions} s WHERE s.uid = :uid', [':uid' => $uid]);

However, is there a built-in service for accessing this table, for greater abstraction?

1 Answer 1

1

You can make use of Dynamic Queries instead of Database::getConnection()->query('SELECT expires_at FROM {sessions} s WHERE s.uid = :uid', [':uid' => $uid]);

You can do:

  // Create an object of type Select.
  $database = \Drupal::database();
  $query = $database->select('sessions', 's');
  // Add extra detail to this query object: a condition, fields and a range.
  $query->condition('s.uid', $uid, '=');
  $query->fields('s', ['expires_at']);
  $result = $query->execute();
2
  • Thanks for the answer :) I assume this means that there is no built-in service I can use specifically for the sessions table?
    – Ynhockey
    Dec 11, 2019 at 12:45
  • No i don't think so, as far as i now there is only Database abstraction layer you can check it here api.drupal.org/api/drupal/…
    – berramou
    Dec 11, 2019 at 13:36

Your Answer

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

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