1
votes

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)

1
give the source of \phpUploadFileTest\phpUploadFileTestBundle\Form\UploadType don't you have somewhere in the code session_start()?Robert
I do not manage session in any code. But Symfony does internally for CSRFuser3502626
I just remember about csrf and when I disable it, I don't get that session error. setting "csrf_protection"=>falseuser3502626
I just review my codes and I used session.user3502626

1 Answers

1
votes

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