-1

How do i port following D7 code to D8?

  drupal_save_session(FALSE);
  session_write_close();
  drupal_save_session(TRUE);

  ini_set('session.cookie_lifetime', $cookie_lifetime);

  drupal_session_started(FALSE);
  drupal_session_initialize();

Note: I need to automatically logout user after $cookie_lifetime (say 30 sec).

I tried following code

$session_manager = \Drupal::service('session_manager');
$session_manager->save(FALSE);
session_write_close();
$session_manager->save(TRUE);
ini_set('session.cookie_lifetime', $cookie_lifetime);

$session_manager->setOptions(array('cookie_lifetime' => $cookie_lifetime));

$session_manager->start();

$session_manager->isStarted();

$session_manager->regenerate();
  • Normally I see code like this associated with how to impersonate a user account during a session. What exactly are you trying to accomplish with this code other than writing/starting a different session? What's the context? – mradcliffe Jul 20 '16 at 11:35
  • What did you try, where are you stuck? – Clive Jul 20 '16 at 11:37
  • I need to automatically logout user after $cookie_lifetime (say 30 sec). – neha Jul 20 '16 at 12:09
  • 1
    You'll need something scheduled then. Cron job, AJAX polling, whatever. Unless you want to manually delay the PHP script for the 30 seconds – Clive Jul 20 '16 at 12:43
  • I tried following code: $session_manager = \Drupal::service('session_manager'); $session_manager->save(FALSE); session_write_close(); $session_manager->save(TRUE); ini_set('session.cookie_lifetime', $cookie_lifetime); Cannot i do similar to way it's done in D-7 without AJAX ? – neha Jul 20 '16 at 13:07
1

Calling user_logout (Drupal 8) will do the trick. This essentially calls $session_manager->destroy(); as well as invoking other hooks. The user_logout function also redirected the user in Drupal 7 making it an undesirable programmatic approach, but this has changed.

// Call this one liner if you like simplicity and depending on services existing on the
// container.
user_logout();

If you want to manually do what user_logout does you can inject the session_manager, logger.factory, module_handler services into your class and call similar methods.

  $user = \Drupal::currentUser();

  // Log some stuff.
  $logger_factory->get('my_module')->notice('User logged out, BAM!');

  // Make sure other modules can clean up after the user!
  $module_handler->invokeAll('user_logout'), [$user]);

  // Destroys the session and removes all cookies.
  $session_manager->destroy();

  // Sets up the anonymous user.
  $user->setAccount(new AnonymousUserSession());

  // And I guess if you will do something else with that anonymous user other than ending
  // the request, then start up and save a session. I don't think you need to do this and I
  // am not sure why you are saving cookie_lifetime on the new anonmyous session.
  // $session_manager->start();
  // $session_manager->setOptions(['cookie_lifetime' => $cookie_lifetime]);
  // $session_manager->save();
| improve this answer | |
0

My working piece of code:

$session_manager = \Drupal::service('session_manager');

$session_manager->setOptions(array('cookie_lifetime' => $cookie_lifetime));

// Force-start a session.
$session_manager->start();
// Check whether a session has been started.
$session_manager->isStarted();
// Migrate the current session from anonymous to authenticated (or vice-versa).
$session_manager->regenerate();
| improve this answer | |

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