Drew Landgrave
Drew Landgrave

Reputation: 1625

Symfony2 : Failed to start the session because headers have already been sent

TL;DR Getting an error on a Linux box with Nginx / PHP-FPM stating "Failed to start the session because headers have already been sent.". Error is not occurring on Apache local machine setup

So on my local machine I have the Symfony2 app running fine. No errors are popping up. But as soon as I deploy to our Linux Server I'm getting this error when I call a certain Action within a Controller class

Failed to start the session because headers have already been sent. 

In the index action I have already called

$session = $this->getRequest()->getSession();

And in another action within the same controller class I'm calling it again. The error pops up when I try a

$session->set('foo', $bar);

In my Twig I'm calling the action by a form and a button with a formaction property like so

<form id='blahblah'>
    .... some fields here .....
    <button type='submit' formaction='{{path('2ndAction')}}'></a>
</form>

So on my local machine, running Apache everything run fine. The Linux server is using Nginx and php-fpm and it's crashing for some reason. I checked the phpInfo() and the session auto start is set to off. Not sure if this is an Nginx/php-fpm issue or not but I thought it may be pertinent information.

Here is the Controller declaration, indexAction(), and my 2ndAction()

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;    
use CBSi\Utils\HTTPUtils\CURLUtil;

class StartController extends Controller
{
    /**
     * @var  CurlUtil $curlUtil
     */
    private $curlUtil;

    /**
     * @var AccessControl $accessControl
     */

    private $accessControl;

    /*placeholder for request object*/
    private $requestHolder;


   /**
    * @Route("/path/for/action/one", name="start")
    * @Template()
    */


public function indexAction()
{
 $session = $this->getRequest()->getSession();
 $this->curlUtil = $this->get('curlUtil');
 $this->requestHolder= Request::createFromGlobals();

// Some logic is done here


return $this->render('ListEngagementBundle:Start:start.html.twig');

}

/**
 * @Route("/path/to/second/action", name="2ndAction")
 * @Template
 */
public function 2ndAction(){
    $session = $this->getRequest()->getSession();
    $this-> curlUtil = $this->get('curlUtil');
    $this->requestHolder= Request::createFromGlobals();

    //Some logic is done here to get the data for the session variable

       $bar= logic output

               $session->set('foo', $bar);

    return $this->redirect($this->generateUrl('start'));
}
}

If you need more info that I can provide I will :)

Upvotes: 8

Views: 42939

Answers (5)

Carlos UCR
Carlos UCR

Reputation: 359

I was getting this error with PHP 8.2, but it got solved getting back to PHP 8.1.17.

Upvotes: 0

sensi
sensi

Reputation: 569

I've get the same error. But in my case I've placed some spaces in the AppKernel.php before the < ?php tag. So if someone else get this error, checkout if you have some spaces or tabs before in first line of each .php file which get loaded before session get initialized.

Upvotes: 7

tr8er
tr8er

Reputation: 94

This happens to me when in some of the scripts that anticipate $this->session->start(); there is a echo statement! Hope this can help someone else debugging the issue

Upvotes: 2

Diego Favero
Diego Favero

Reputation: 2115

I was getting this error message every time I tried to update my database schema using symfony console and when I tried to install new dependencies using composer:

[RuntimeException]

Failed to start the session because headers have already been sent by "/var    
/www/html/pulsar283/src/MyService/Local/Brasil.php" at line 153.

So, I went for check the file , and, on line 152 I found a " ?> " (php close tag ).

So, to I just remove the php close tag and the error never shown again !

Upvotes: 0

Drew Landgrave
Drew Landgrave

Reputation: 1625

So I figured it out. In the 2nd action where I was calling

$session->getRequest()->getSession(); 

I had to change that to

$session = new Session();
$session->start();

Go figure. :P

Upvotes: 9

Related Questions