phpsymfonysymfony-2.8

Symfony form Failed to start the session: already started by PHP


I got that error Failed to start the session: already started by PHP. when sending a form view to Twig. I don't know why and I would like to know.

Action

public function indexAction(Request $request) {

    $em = $this->getDoctrine()->getManager();

    $uploadFormType = new \phpUploadFileTest\phpUploadFileTestBundle\Form\UploadType();
    $uploadEntity= new \phpUploadFileTest\phpUploadFileTestBundle\Entity\Upload();
    $uploadForm = $this->createForm($uploadFormType, $uploadEntity );
    $uploadForm->handleRequest($request);


    if($uploadForm->isSubmitted() && $uploadForm->isValid()){



        $em->persist($uploadEntity);
        $em->flush();


    }
    return $this->render("phpUploadFileTestphpUploadFileTestBundle::index.html.twig", array("uploadForm" => $uploadForm->createView()));

}

The error happens from that code $uploadForm->createView(). When I remove it, I don't get that error.

\phpUploadFileTest\phpUploadFileTestBundle\Form\UploadType->buildForm code:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('name', "text",[])->add('file', "text",[]);
}

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.

I don't know about the session management. But the error thrown from vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php at line 132:

if (PHP_VERSION_ID >= 50400 && \PHP_SESSION_ACTIVE === session_status()) {
        throw new \RuntimeException('Failed to start the session: already started by PHP.');
    }

from in /home/guervyl/NetBeansProjects/gvwb/website-creator-symfony/vendor/symfony/symfony/src/Symfony/Component/Security/Csrf/TokenStorage/SessionTokenStorage.php at line 90:

public function hasToken($tokenId)
{
    if (!$this->session->isStarted()) {
        **$this->session->start();**
    }
    return $this->session->has($this->namespace.'/'.$tokenId);

I created a custom app_dev.php. In it I started a session. I used the legacy session and session and I'm still getting that error.

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->start();

...
$session->set(...);
...
$session->has(...);
...
$session->get(...);

My Symfony version is 2.8.20 (I know it's old)


Solution

  • I fixed it by adding storage_id: session.storage.php_bridge into my config.yml under:

    framework:
        session: { handler_id: null, storage_id: session.storage.php_bridge}
    

    Then in my php file I use the Symfony Session class:

    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->start();
    
    ...
    $session->set(...);
    ...
    $session->has(...);
    ...
    $session->get(...);
    

    From there then there