Search code examples
sessiondrupal-8

Is it ok to use $_SESSION['variables'] in Drupal 8?


I need a way to store temporary data for anonymous users.

Apparently this is not possible with:

\Drupal::service('user.private_tempstore')

Unless you write a custom constructor for the session management and stuff, which seems a little far-fetched to me?

I tried using

\Drupal::service('user.shared_tempstore')

But that saves the temp data for all anonymous users. So it's not linked to a single user.

Using raw $_SESSION['data'] works fine, but I'm not sure if I'm supposed to be doing this in Drupal and how safe/unsafe it is to do this?


Solution

  • I've been using plain PHP $_SESSION variables for a while now. Did some research on them and they should be perfectly safe to use. They're working correctly everywhere I use them and they have been working correctly for a while.

    Don't think there's any issue using them in Drupal 8.