Symfony2: Could not start session because headers have already been sent

TL DR Getting an error in a Linux box with Nginx / PHP-FPM with the message "The session could not start because the headers have already been sent." Error installing local Apache machine

So, on my local machine, the Symfony2 application is working fine. No errors appear. But as soon as I deploy to our Linux server, I get this error when I call a specific action in the controller class

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

In the index action, I have already named

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

And in another action within the same controller class, I call it again. An error appears when I try to execute

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

In my Twig, I invoke an action with a form and a button with the same formaction property

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

So, on my local machine running Apache, everything is working fine. The Linux server uses Nginx and php-fpm, and for some reason it crashes. I checked phpInfo () and automatic session start is disabled. Not sure if this is a Nginx / php-fpm problem or not, but I thought it might be relevant 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 information that I can provide, I will :)

+6
source share
3 answers

So, I figured it out. In the second action, where I called

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

I had to change this to

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

Move figure .: P

+4
source

I have some error. But in my case, I placed some spaces in AppKernel.php before the < ?php tag. Therefore, if someone else gets this error, check to see if you have spaces or tabs in the first line of each .php file that are loaded before the session initializes.

+4
source

I received 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 to check the file, and on line 152 I found "?>" (Php close tag).

So, I just remove the php close tag and the error no longer appears!

0
source

All Articles