Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :)

like image 662
Drew Landgrave Avatar asked Oct 29 '12 17:10

Drew Landgrave


1 Answers

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

like image 113
Drew Landgrave Avatar answered Sep 26 '22 16:09

Drew Landgrave