Skip to content

[HttpFoundation] Emit PHP warning when Response::sendHeaders() is called while output has already been sent #60377

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

Merged
merged 1 commit into from
May 10, 2025

Conversation

ivo95v
Copy link
Contributor

@ivo95v ivo95v commented May 7, 2025

Q A
Branch? 7.3
Bug fix? no
New feature? no
Deprecations? no
Issues -
License MIT

Description:

When Symfony attempts to send HTTP headers via the sendHeaders() method, it silently aborts if headers have already been sent (e.g., due to whitespace before the <?php tag). I spent an entire day debugging this issue and discovered that the problem was caused by an unintended whitespace before a PHP opening tag. To avoid this kind of silent failure and make future debugging easier, I decided to add an error log.

@carsonbot carsonbot added this to the 7.3 milestone May 7, 2025
@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 7.3 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@ivo95v ivo95v changed the title added error log to symfony Response sendHeaders [HttpFoundation][Response]added error log to symfony Response sendHeaders May 7, 2025
@ivo95v ivo95v changed the title [HttpFoundation][Response]added error log to symfony Response sendHeaders [HttpFoundation][Response] added error log to symfony Response sendHeaders May 7, 2025
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt calling error_log is fine: we should use the logger infrastructure instead.
But Response is not a service, it doesn't fit.
Then, I'm wondering which service could be responsible for doing this check and logging the issue.
Maybe ErrorListener since it already has a logger injected? Any other idea?

@OskarStark OskarStark changed the title [HttpFoundation][Response] added error log to symfony Response sendHeaders [HttpFoundation][Response] added error log to Response::sendHeaders() May 9, 2025
@nicolas-grekas
Copy link
Member

nicolas-grekas commented May 9, 2025

I guess that's a possiblity that would work:
instead of the added line, we'd just do
header(\sprintf('HTTP/%s %s %s', $this->version, $statusCode, $this->statusText), true, $statusCode); and let PHP generate the proper warning.

@ivo95v up to update your PR?
Please mind the code style, indentation is broken at the moment.

@ivo95v
Copy link
Contributor Author

ivo95v commented May 9, 2025

thank you @nicolas-grekas, sending header instead of error_log is a good idea. I just update the PR

@carsonbot carsonbot changed the title [HttpFoundation][Response] added error log to Response::sendHeaders() [HttpFoundation] [Response] added error log to Response::sendHeaders() May 10, 2025
@nicolas-grekas nicolas-grekas changed the title [HttpFoundation] [Response] added error log to Response::sendHeaders() [HttpFoundation] Emit PHP warning when Response::sendHeaders() while output has already been sent May 10, 2025
@nicolas-grekas nicolas-grekas force-pushed the fix-missing-error-log branch from 6359c43 to a69cf15 Compare May 10, 2025 08:44
@nicolas-grekas nicolas-grekas changed the title [HttpFoundation] Emit PHP warning when Response::sendHeaders() while output has already been sent [HttpFoundation] Emit PHP warning when Response::sendHeaders() is called while output has already been sent May 10, 2025
@fabpot
Copy link
Member

fabpot commented May 10, 2025

Thank you @ivo95v.

@fabpot fabpot merged commit cf554e1 into symfony:7.3 May 10, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants