【问题标题】:Symfony2 : Failed to start the session because headers have already been sentSymfony2:无法启动会话,因为标头已经发送
【发布时间】:2012-10-19 02:12:10
【问题描述】:

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

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

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

在我已经调用的索引操作中

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

在同一个控制器类的另一个动作中,我再次调用它。当我尝试时弹出错误

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

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

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

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

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

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

【问题讨论】:

标签: apache symfony nginx php


【解决方案1】:

所以我想通了。在我打电话的第二个动作中

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

我不得不把它改成

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

去图吧。 :P

【讨论】:

  • 是的,当您比较 S2.0 和 S2.1 时,会话似乎有很多变化;)
  • Symfony 5 中的类似问题和类似解决方案
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      每当我尝试使用 symfony 控制台更新我的数据库架构以及尝试使用 composer 安装新的依赖项时,我都会收到此错误消息:

      [运行时异常]

      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 关闭标记,并且错误不再显示!

      【讨论】:

      • 结束标记后可能还有其他内容,例如空格、换行符等。否则,单独的结束标记不可能产生错误。这就是为什么默认省略关闭标签的原因 - 以防止关闭标签后出现废话。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      • 2019-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-15
      相关资源
      最近更新 更多