Mention where headers were already sent if session_start fails (#16378)

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
This commit is contained in:
Calvin Buckley 2024-10-14 21:13:43 -03:00 committed by GitHub
parent 275c7f21ab
commit edf351ce6d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 1 deletions

View File

@ -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;
}

View File

@ -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