【问题标题】:Symfony 5 - Failed to start the session because headers have already been sent by "php://input" at line 1 in productionSymfony 5 - 无法启动会话,因为标头已由“php://input”在生产中的第 1 行发送
【发布时间】:2021-02-27 02:41:05
【问题描述】:

*** 编辑 - 已解决 ***

万一有一天它可能对某人有所帮助,真正的原因是我的服务器上的恶意软件正在通过我的 php-fpm 容器挖掘加密货币。

如果你想检查:

  • 如果尚未安装 htop,请将其安装到您的服务器
  • 寻找你的 CPU 利用率,如果有一个程序正在使用它
  • 删除它
  • 使用网络保护您的 docker,不要打开端口 9000

就我而言,就是这个“devtmpfsi”

kdevtmpfsi using the entire CPU

*** 编辑结束 ***


我知道这个错误就像世界一样古老,但在每个不同的网站和主题上搜索了大约三天的解决方案后,我找不到任何符合我情况的内容。

所以我希望有人可以帮助我。

上下文:

  • 我正在使用 JS 中的函数 fetch 来获取我的数据
  • 我在开发和生产环境中使用 docker 和 nginx (1.10.3) 和 php-fpm (PHP: 7.4.12)
  • 在开发环境中一切正常
  • 大约几分钟/或在多次刷新页面后发生,但是当我重新启动 docker 时一切正常,然后再次发生

错误截图

First Screenshot

Second screenshot

代码

JS:

const fetchSkinWithParameters = () => {
        fetch(platformUrl + '/api/skins', {
            method: "POST",
            body: JSON.stringify({
                currentIndex: containerPage1.dataset.currentindex,
                type: containerPage1.dataset.currenttype,
            })
        }).then((res) => {
            return res.json();
        }).then((data) => {
            data = data.data;
            name = data.name;
            totalSkin = data.totalSkin;
            id = data.id;

            skinSelectorPreview.dataset.id = id.toString();
            skinSelectorPreview.setAttribute('src', '/assets/images/skins/' + name);
        });
    }

PHP(symfony 5):

 /**
 * @Route("/api/skins"), name="choose_skin"
 */
public function chooseSkin(Request $request)
{
    $message = json_decode($request->getContent(), true);

    $type = $message['type'];

    $em = $this->getDoctrine();

    $skins = $em->getRepository(Skin::class)->findByType($type);

    $currentIndex = $message['currentIndex'];

    $data = [
        'id' => $skins[$currentIndex]->getId(),
        'name' => $skins[$currentIndex]->getName(),
        'totalSkin' => count($skins),
    ];

    $response = new Response();
    $response->setContent(json_encode([
        'data' => $data,
    ]));
    $response->headers->set('Content-Type', 'application/json');
    $response->setStatusCode(200);

    return $response;
}

我尝试过的解决方案:

  • 修改我的 php.ini (output_buffering)
  • 检查我是否在任何 之前没有愚蠢的空格
  • 还有很多关于会话的小事(以此类推)

这是我在这里的第一篇文章,因为我总是在这里找到解决方案,我现在很生气:/

我会提供你需要的任何信息/如果你问我忘了在这里输入

感谢您的宝贵时间。

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    在你的 JS 中添加 headers 这样的方式:

    fetch(platformUrl + '/api/skins', {
        method: "POST",
        headers: {
             'Accept': 'application/json, text/plain, */*',
             'Content-Type': 'application/json'
        },
        body: JSON.stringify({
              currentIndex: containerPage1.dataset.currentindex,
              type: containerPage1.dataset.currenttype,
        })
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-29
      • 2013-05-15
      相关资源
      最近更新 更多