2012-10-29 41 views
6

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')); 
} 
} 

如果您需要更多信息,我可以提供我會:)

+1

選中此http:// stackoverflow。com/questions/8028957/warning-headers-already-sent-in-php –

回答

4

所以我想通了。在我打電話

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

的第二招,我不得不改變,要

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

進入數字。 :P

+0

是的,當你比較S2.0和S2.1時,會話似乎有一點變化;) –

4

我得到了一些錯誤。但在我的情況下,我已經在標記之前的AppKernel.php中放置了一些空格。因此,如果其他人遇到此錯誤,請檢查在每個.php文件的第一行之前是否有一些空格或製表符,它們在會話初始化之前加載。

0

每次我試圖用symfony的控制檯來更新我的數據庫架構時,我收到此錯誤信息,當我試圖用作曲家來安裝新的依賴:

[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. 

所以,我去檢查文件,並在第152行,我發現了一個「?>」(PHP關閉標籤)。

因此,我只是刪除php關閉標籤和錯誤再也沒有顯示!

相關問題