Skip to content

headers already sent by ... Response.php:1315 / SessionUtils.php:52 #59205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
THenkeDE opened this issue Dec 13, 2024 · 7 comments
Closed

headers already sent by ... Response.php:1315 / SessionUtils.php:52 #59205

THenkeDE opened this issue Dec 13, 2024 · 7 comments

Comments

@THenkeDE
Copy link

Symfony version(s) affected

6.4.*

Description

My prod.log is full with always the same Line:

php.WARNING: Warning: Cannot modify header information - headers already sent by (output started at localpath/vendor/symfony/http-foundation/Response.php:1315) {"exception":"[object] (ErrorException(code: 0): Warning: Cannot modify header information - headers already sent by (output started at localpath/vendor/symfony/http-foundation/Response.php:1315) at localpath/vendor/symfony/http-foundation/Session/SessionUtils.php:52)"} []

The causing Line (SessionUtils.php:52) removes the cookie Header

header_remove('Set-Cookie');

and the error is generated while flushing the output buffer

ob_end_flush();

Is there anything i can do to fix this?

How to reproduce

sadly i have no Idea, it is just getted logged

Possible Solution

No response

Additional Context

No response

@andriusvo
Copy link

Same issue here. So it's relevant.

@xabbuh
Copy link
Member

xabbuh commented Dec 13, 2024

Well, there's not much information to help you I am afraid. We will probably need a small example application that allows to debug.

@andriusvo
Copy link

@THenkeDE Some questions we could minimze the scope of the problem?

  • Are you using EasyAdminBundle? (Because we are using, so maybe the issue is here)
  • Are you setting custom cookies?

@Renrhaf
Copy link

Renrhaf commented Jun 4, 2025

Same issue here, it happens when streaming a PDF (using DOMPDF) to the browser
Warning: Cannot modify header information - headers already sent by (output started at /app/vendor/dompdf/dompdf/src/Adapter/CPDF.php:982)

Output started at : https://github.com/dompdf/dompdf/blob/master/src/Adapter/CPDF.php#L982
Warning popping at : https://github.com/symfony/symfony/blob/7.4/src/Symfony/Component/HttpFoundation/Session/SessionUtils.php#L52

EDIT: I just changed to the Symfony way, ie: sending a binary response instead of using directly the "stream" method of the DOMPDF lib which is outputing content, causing the error with the kernel response AbstractSessionListener.

@nicolas-grekas
Copy link
Member

We'd need a reproducer to understand the issue.
To anyone seeing the same problem, please create a small app that one could run to debug this.
Skip "me too" comments unless you post a reproducer please :)

@k00ni
Copy link

k00ni commented Jun 4, 2025

PHP: 8.3, Symfony: 7.3.0


I have exceptions in my logs which look very similar:

{"message":"Uncaught PHP Exception ErrorException: \"Warning: Cannot modify header information - headers
already sent by (output started at /var/www/html/src/App/Controller/ShowLocalFileController.php:29)\" 
at Response.php line 322","context":{"exception":{"class":"ErrorException","message":
"Warning: Cannot modify header information - headers already sent by (output started at 
/var/www/html/src/App/Controller/ShowLocalFileController.php:29)","code":0,"file":
"/var/www/html/vendor/symfony/http-foundation/Response.php:322"}},
"level":500,"level_name":"CRITICAL","channel":"request","datetime":"2025-06-04T11:56:38.706342+00:00",
"extra":{}}

The related code part is:

// code part simplified to demonstrate the case

    #[Route('/', name: 'home')]
    public function handleRequest(): Response
    {
        $filepath = ROOT_PATH.'mbl-2025-7.pdf';

        // load file and show in browser
        header('Content-type:'. mime_content_type($filepath));
        echo $this->filesystem->readFile($filepath);  // <======== line 29, is mentioned in the error log

        return new Response();
    }

Here is a reproducer based on the code of an proprietary app, so I hope it matches the case of the author and I extracted only the relevant code parts.

https://github.com/k00ni/cannot-modify-header-reproducer

I can provider further details if they are needed. The PDF file inside the repository is public domain by German law, in case that is relevant.


Workaround?

I solved it by replacing the mentioned code part with Symfony's BinaryFileResponse. So the code part has to be changed to:

// code part simplified to demonstrate the case

    #[Route('/', name: 'home')]
    public function handleRequest(): Response
    {
        $filepath = ROOT_PATH.'mbl-2025-7.pdf';

        $response = new \Symfony\Component\HttpFoundation\BinaryFileResponse($filepath);
        return $response;
    }

Symfony documentation provides further information about this class: https://symfony.com/doc/current/components/http_foundation.html#serving-files

@nicolas-grekas
Copy link
Member

Thanks for the reproducer. Actually, the workaround you posted is a fix. The previous code was just broken and worked by chance.

Let me close.
Note that there's #60603 with some related follow up, but linked to a recent change in 7.3, which is what you might be experiencing @k00ni

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy