Why 'Failed to start the session' when RedirectResponse is sent? When this code is executed, I have an error message logged.
The code is in shared_node_access(NodeInterface $node, $op, AccountInterface $account)

if ($node-&gt;get('nid')-&gt;value == 1) {
  if ($account-&gt;isAnonymous()) {
  }
  else {
    $path = Url::fromUserInput('/node/25')-&gt;toString();
    $response = new RedirectResponse($path);
    $response-&gt;send();
  }
}


The error message:


  RuntimeException : Failed to start the session because headers have
  already been sent by
  "/home/lejardindf/JdP8/vendor/symfony/http-foundation/Response.php" at
  line 377. dans
  Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage->start()
  (/home/lejardindf/JdP8/vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php
  ligne 150)
There are two problems. First, you can't send a response from custom Drupal code. Second, you can't run non access code in an access hook, because access results are cached.

For redirects in Drupal 8 see these change records: drupal_goto() has been removed and hook_init() removed.

A different approach might be to let the 403 exception happen and then catch it in an exception event subscriber to do the redirect, see How to redirect an anonymous user to the login form after a 403 error?