Request referer gets cached I need to grab referer and save it for the first time (I save it to sessions) and when the user gets to a specific page forward it. My problem is that after opening page with the same referer few times, I am not getting referer anymore. I tried it with few different browsers in private/incognito mode but after a few tries it gets cached and does not return anymore. My Code :

function HOOK_template_preprocess_default_variables_alter(&amp;$variables)
{
    $request = Drupal::request();

    var_dump($request-&gt;headers-&gt;get('referer')); //stops dumping after few times

    $_SESSION['referer']['url'] = $request-&gt;headers-&gt;get('referer');
    $variables['#cache']['contexts'][] = 'headers:referer';
}


Is there a way to save referer for every anonymous user that comes to website?
For doing this, i won't use a hook but the drupal eventSubscriber :

First your need to make a custom module with the file for declare to drupal your new service :


  myModule.services.yml


services:
  myModule.services:
    class: Drupal\myModule\EventSubscriber\MyServiceSubscriber
    arguments: []
    tags:
      - { name: event_subscriber }


Then in the file :


  myModule/src/EventSubscriber/MyServiceSubscriber.php


&lt;?php

namespace Drupal\myModule\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

    /**
     * Class myServiceSubscriber.
     *
     * @package Drupal\myModule
     */
    class MyServiceSubscriber implements EventSubscriberInterface {

        public function onRequest(GetResponseEvent $event) {
           // here your code, this method ll be call on every call request
           // so you can code your logic here
        }


      /**
       * {@inheritdoc}
       */
      static function getSubscribedEvents() {

          $events[KernelEvents::REQUEST][] = ['onRequest', 900];

          return $events;
      }

    }