php Symfony2:无法启动会话,因为头已经发送

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13127122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:49:24  来源:igfitidea点击:

Symfony2 : Failed to start the session because headers have already been sent

apachesymfonynginxphp

提问by Drew Landgrave

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

TL;DR 在带有 Nginx/PHP-FPM 的 Linux 机器上出现错误,指出“无法启动会话,因为标头已经发送。”。Apache 本地机器设置未发生错误

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

所以在我的本地机器上,我的 Symfony2 应用程序运行良好。没有错误弹出。但是,一旦我部署到我们的 Linux 服务器,当我在 Controller 类中调用某个 Action 时,就会收到此错误

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

在我的 Twig 中,我通过一个表单和一个具有 formaction 属性的按钮来调用操作,就像这样

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

所以在我的本地机器上,运行 Apache 一切正常。Linux 服务器正在使用 Nginx 和 php-fpm,但由于某种原因它崩溃了。我检查了 phpInfo() 并且会话自动启动设置为关闭。不确定这是否是 Nginx/php-fpm 问题,但我认为这可能是相关信息。

Here is the Controller declaration, indexAction(), and my 2ndAction()

这是控制器声明、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'));
}
}

If you need more info that I can provide I will :)

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

采纳答案by Drew Landgrave

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

去搞清楚。:P

回答by sensi

I've get the same error. But in my case I've placed some spaces in the AppKernel.php before the < ?phptag. 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.

我遇到了同样的错误。但就我而言,我在 AppKernel.php 中的< ?php标记前放置了一些空格。因此,如果其他人收到此错误,请检查在会话初始化之前加载的每个 .php 文件的第一行之前是否有一些空格或制表符。

回答by Diego Favero

I was getting 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:

每次我尝试使用 symfony 控制台更新我的数据库模式以及尝试使用 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.

[运行时异常]

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

所以,我去检查文件,并且在第 152 行我发现了一个“?>”(php 关闭标签)。

So, to I just remove the php close tag and the error never shown again !

所以,我只是删除了 php 关闭标签,错误再也没有出现过!

回答by fmariano

This happens to me when in some of the scripts that anticipate $this->session->start(); there is a echostatement! Hope this can help someone else debugging the issue

当在一些预期$this->session->start(); echo语句的脚本中时,我就会发生这种情况!希望这可以帮助其他人调试问题