TL; DR 使用Nginx/PHP-FPM在Linux機器上獲取錯誤,指出「由於標題已被髮送,未能啓動會話」。錯誤沒有發生在Apache本地機器上設置Symfony2:由於標題已被髮送而無法啓動會話
所以在我的本地機器上,我的Symfony2應用程序運行良好。沒有錯誤出現。但只要我部署到我們的Linux服務器,當我打電話控制器類
Failed to start the session because headers have already been sent.
中的某個操作在index動作我已經呼籲
$session = $this->getRequest()->getSession();
而且在我得到這個錯誤同一控制器類中的另一個動作,我再次調用它。錯誤彈出,當我嘗試
$session->set('foo', $bar);
在我的枝條我打電話的形式動作和一個按鈕與formaction財產,像這樣
<form id='blahblah'>
.... some fields here .....
<button type='submit' formaction='{{path('2ndAction')}}'></a>
</form>
所以我的本地機器上,運行阿帕奇一切運行良好。 Linux服務器使用Nginx和php-fpm,並且由於某種原因它崩潰了。我檢查了phpInfo()並將會話自動啓動設置爲關閉。不知道這是否是Nginx/php-fpm問題,但我認爲這可能是相關的信息。
這裏是控制器的聲明,的indexAction(),和我的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'));
}
}
如果您需要更多信息,我可以提供我會:)
選中此http:// stackoverflow。com/questions/8028957/warning-headers-already-sent-in-php –