How can I use $_SESSION in form handler drupal 8? I create a module that has form . I want send data from other form to this form and show them . How can I save previous data in my form and show new data ?

For example I send number 1 to my form and form show 1 , then send number 2 and form show 1 and 2 , and then send number 3 and my form show 1 and 2 and 3 .

Then I want send this data ( 1 and 2 and 3 and ... ) to other page with my form . how can I do this ?

My sender form :

&lt;form action="/form" method="post"&gt;
   &lt;input type="text" name="number" /&gt;
   &lt;input type="submit" value="GO" /&gt;
&lt;/form&gt;
The suberglobal $_SESSION still works in D8 and is used by core several times. 

For a correct Drupal 8 way see this change record. It shows how you access the session variable through the request object. But it doesn't show how to get the request object.

So this is an example for accessing the session in a form handler:

use Symfony\Component\HttpFoundation\Request;

  public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {

    $session = $request-&gt;getSession();
    $value = $session-&gt;get('mymodule_count', 0);
    $session-&gt;set('mymodule_count', $value + 1);
    $form['counter'] = ['#markup' =&gt; '&lt;p&gt;#' . $value . '&lt;/p&gt;'];


In a controller or form you can pull the request from the route parameter stack by putting a typed parameter Request $request in the build method.

If you are in a procedural form function get the request from \Drupal:

$request = \Drupal::request();