'how can I set a session variable in Drupal 8 and get it in a php script?

I'm working in a custom module for drupal 8. I have a form and I'm trying to send by means of a submit button some data from my custom module to another php script file:

$form['submit2'] = [
        '#type' => 'submit',
        '#value' => t('editor'),
        '#access' =>isset($_GET['id']) ? true:false,
        '#submit' => ['::goto_editor']


];  

....

  public function goto_editor() {

    $session =  \Drupal::request()->getSession();
    $session->set('value', 'hi');

  } 

Now I want to get the value in another PHP file. How can I connect my PHP script with the Drupal 8.x session service? Thanks a lot.



Solution 1:[1]

Would the private tempstore do what you need? In your case perhaps you might set the value like this:

$tempstore = \Drupal::service('tempstore.private')->get('your_module_name');
$tempstore->set('value', 'hi');

Then in your second PHP file get it like this:

$tempstore = \Drupal::service('tempstore.private')->get('your_module_name');
$some_data = $tempstore->get('value');

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RhiP