diff --git a/ext/session/session.c b/ext/session/session.c index 3d05e9efd3111..8f0acdbcd892b 100644 --- a/ext/session/session.c +++ b/ext/session/session.c @@ -2644,7 +2644,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; } diff --git a/ext/session/tests/gh-16372.phpt b/ext/session/tests/gh-16372.phpt new file mode 100644 index 0000000000000..7ce572792bc3f --- /dev/null +++ b/ext/session/tests/gh-16372.phpt @@ -0,0 +1,19 @@ +--TEST-- +GH-16372: Mention where headers were already sent if session_start fails +--EXTENSIONS-- +session +--SKIPIF-- + +--FILE-- + +--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