Symfony2 : Failed to start the session because headers have already been sent
Solution 1
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
Solution 2
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.
Solution 3
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
Drew Landgrave
Updated on July 09, 2022Comments
-
Drew Landgrave almost 2 years
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 :)