Skip to content

Commit

Permalink
Mention where headers were already sent if session_start fails (#16378)
Browse files Browse the repository at this point in the history
We had previously improved where sessions were already started, and
where headers were already sent when setting headers, but not where a
header has been sent if we try to set the header cookie.

Fixes GH-16372
  • Loading branch information
NattyNarwhal authored Oct 15, 2024
1 parent 275c7f2 commit edf351c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
9 changes: 8 additions & 1 deletion ext/session/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -2654,7 +2654,14 @@ PHP_FUNCTION(session_start)
* module is unable to rewrite output.
*/
if (PS(use_cookies) && SG(headers_sent)) {
php_error_docref(NULL, E_WARNING, "Session cannot be started after headers have already been sent");
/* It's the header sent to blame, not the session in this case */
const char *output_start_filename = php_output_get_start_filename();
int output_start_lineno = php_output_get_start_lineno();
if (output_start_filename != NULL) {
php_error_docref(NULL, E_WARNING, "Session cannot be started after headers have already been sent (sent from %s on line %d)", output_start_filename, output_start_lineno);
} else {
php_error_docref(NULL, E_WARNING, "Session cannot be started after headers have already been sent");
}
RETURN_FALSE;
}

Expand Down
19 changes: 19 additions & 0 deletions ext/session/tests/gh-16372.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
GH-16372: Mention where headers were already sent if session_start fails
--EXTENSIONS--
session
--SKIPIF--
<?php include('skipif.inc'); ?>
--FILE--
<?php

header("X-PHP-Test: test");

echo "Sent headers\n";

session_start();
?>
--EXPECTF--
Sent headers

Warning: session_start(): Session cannot be started after headers have already been sent (sent from %s on line %d) in %s on line %d

0 comments on commit edf351c

Please sign in to comment.