title: Symfony form Failed to start the session: already started by PHP
body: <p>I got that error <strong>Failed to start the session: already started by PHP.</strong> when sending a form view to Twig. I don't know why and I would like to know.</p>
  
  <p>Action</p>
  
  <pre><code>public function indexAction(Request $request) {
  
      $em = $this-&gt;getDoctrine()-&gt;getManager();
  
      $uploadFormType = new \phpUploadFileTest\phpUploadFileTestBundle\Form\UploadType();
      $uploadEntity= new \phpUploadFileTest\phpUploadFileTestBundle\Entity\Upload();
      $uploadForm = $this-&gt;createForm($uploadFormType, $uploadEntity );
      $uploadForm-&gt;handleRequest($request);
  
  
      if($uploadForm-&gt;isSubmitted() &amp;&amp; $uploadForm-&gt;isValid()){
  
  
  
          $em-&gt;persist($uploadEntity);
          $em-&gt;flush();
  
  
      }
      return $this-&gt;render("phpUploadFileTestphpUploadFileTestBundle::index.html.twig", array("uploadForm" =&gt; $uploadForm-&gt;createView()));
  
  }
  </code></pre>
  
  <p>The error happens from that code <strong><code>$uploadForm-&gt;createView()</code></strong>. When I remove it, I don't get that error.</p>
  
  <p>\phpUploadFileTest\phpUploadFileTestBundle\Form\UploadType->buildForm code:</p>
  
  <pre><code>public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $builder-&gt;add('name', "text",[])-&gt;add('file', "text",[]);
  }
  </code></pre>
  
  <p>That error fixed when I disable it the CSRF on the form, setting "csrf_protection"=>false. But I don't want to disable the csrf protection.</p>
  
  <p>I don't know about the session management. But the error thrown from <code>vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php at line 132</code>:</p>
  
  <pre><code>if (PHP_VERSION_ID &gt;= 50400 &amp;&amp; \PHP_SESSION_ACTIVE === session_status()) {
          throw new \RuntimeException('Failed to start the session: already started by PHP.');
      }
  </code></pre>
  
  <p>from in /home/guervyl/NetBeansProjects/gvwb/website-creator-symfony/vendor/symfony/symfony/src/Symfony/Component/Security/Csrf/TokenStorage/SessionTokenStorage.php at line 90:</p>
  
  <pre><code>public function hasToken($tokenId)
  {
      if (!$this-&gt;session-&gt;isStarted()) {
          **$this-&gt;session-&gt;start();**
      }
      return $this-&gt;session-&gt;has($this-&gt;namespace.'/'.$tokenId);
  </code></pre>
  
  <p>I created a custom app_dev.php. In it I started a session. I used the <a href="http://symfony.com/doc/2.8/components/http_foundation/session_php_bridge.html" rel="nofollow noreferrer">legacy session</a> and <a href="http://symfony.com/doc/2.8/components/http_foundation/sessions.html" rel="nofollow noreferrer">session</a> and I'm still getting that error.</p>
  
  <pre><code>use Symfony\Component\HttpFoundation\Session\Session;
  use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
  
  // legacy application configures session
  ini_set('session.save_handler', 'files');
  ini_set('session.save_path', '/tmp');
  session_start();
  
  // Get Symfony to interface with this existing session
  $session = new Session(new PhpBridgeSessionStorage());
  $session-&gt;start();
  
  ...
  $session-&gt;set(...);
  ...
  $session-&gt;has(...);
  ...
  $session-&gt;get(...);
  </code></pre>
  
  <p>My Symfony version is 2.8.20 (<em>I know it's old</em>)</p>