Storing Session Data with Drupal 8

 The Drupal session management subsystem is built on top of the Symfony session component. It is optimized in order to minimize the impact of anonymous sessions on caching proxies. A session is only started if necessary and the session cookie is removed from the browser as soon as the session has no data. For this reason it is important for contributed and custom code to remove session data if it is not used anymore.



Session data is accessed via the \Symfony\Component\HttpFoundation\Request::getSession() method, which returns an instance of \Symfony\Component\HttpFoundation\Session\SessionInterface. The most important methods on SessionInterface are set(), get(), and remove().


The following code fragment shows the implementation of a counter controller relying on the session:



public function counter(Request $request) {

  $session = $request->getSession();

  $count = $session->get('mymodule.counter', 0) + 1;

  $session->set('mymodule.counter', $count);


  return [

    '#markup' => $this->t('Page Views: @count', ['@count' => $count]),

    '#cache' => [

      'max-age' => 0,

    ],

  ];

}


public function reset(Request $request) {

  $session = $request->getSession();

  $session->remove('mymodule.counter');

}

It is important to keep the amount of data stored inside the session to a minimum, as the complete session is loaded on every request. Also third party session storage backends do not necessarily allow objects of unlimited size. If it is necessary to collect a non-trivial amount of data specific to a user's session, use the Key/Value store to save the serialized data and only store the key to the entry in the session.


In Drupal 8, there is a need to store information associated with a user's session. There are two services for temporarily storing user-specific and non-user-specific data in key/value format, namely, use user.private_tempstore and user.shared_tempstore.

TempStore

A tempstore is for data that needs to be persisted between requests without being saved back to the canonical storage (such as an entity or configuration object)

Two methods can be effectively used for  private_tempstore - Use "\Drupal::service()" and Dependency Injection.

Use "\Drupal::service()"

To set Temporary Data

$tempstore = \Drupal::service('user.private_tempstore')->get('mymodule_name');

$tempstore->set('my_variable_name', $some_data);

To read Temporary Data

$tempstore = \Drupal::service('user.private_tempstore')->get('mymodule_name'); 

$tempstore->get('my_variable_name', $some_data);

PrivateTempStore

A PrivateTempStore can be used to make temporary, non-cache data available across requests. The data for the PrivateTempStore is stored in one key/value collection. PrivateTempStore data expires automatically after a given timeframe.

Dependency Injection

To use this service need to invoke the following “use” statement

use Drupal\user\PrivateTempStoreFactory;           

use Symfony\Component\DependencyInjection\ContainerInterface;

Temporary store - that is private to the current user



Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...