user3502626
user3502626

Reputation: 838

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)

Upvotes: 2

Views: 4203

Answers (2)

Staffel023
Staffel023

Reputation: 1

Symfony sessions are incompatible with the php.ini directive session.auto_start = 1 This directive must be disabled in php.ini, web server directives or .htaccess. Taken from: https://symfony.com/doc/current/session.html

A quick solution to this problem is:

  1. Go to the path where you save PHP.
  2. Open php.ini
  3. Find session.auto_start
  4. Change value 1 to 0
  5. Restart your Symphony app

I'm not sure if this solution to be validate for everyone, but give it a try.

Upvotes: 0

user3502626
user3502626

Reputation: 838

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

Upvotes: 2

Related Questions