From 30081d74d6c497f0bee2ff04281faa047728d6f1 Mon Sep 17 00:00:00 2001 From: Calvin Buckley Date: Fri, 11 Oct 2024 16:53:09 -0300 Subject: [PATCH 1/3] Mention where headers were already sent if session_start fails 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 --- ext/session/session.c | 9 ++++++++- ext/session/tests/gh-16372.phpt | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 ext/session/tests/gh-16372.phpt diff --git a/ext/session/session.c b/ext/session/session.c index 3d05e9efd3111..cb7d3fd18b182 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_NOTICE, "Session cannot be started after headers have already been sent (started from %s on line %d)", output_start_filename, output_start_lineno); + } else { + php_error_docref(NULL, E_NOTICE, "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..d526d51d867e2 --- /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 + +Notice: session_start(): Session cannot be started after headers have already been sent (started from %s on line %d) in %s on line %d From e925c365400f1641920d5b900dca51d8efdf3cc6 Mon Sep 17 00:00:00 2001 From: Calvin Buckley Date: Mon, 14 Oct 2024 13:46:00 -0300 Subject: [PATCH 2/3] This should be a warning like before --- ext/session/session.c | 4 ++-- ext/session/tests/gh-16372.phpt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/session/session.c b/ext/session/session.c index cb7d3fd18b182..020fa19a2e59e 100644 --- a/ext/session/session.c +++ b/ext/session/session.c @@ -2648,9 +2648,9 @@ PHP_FUNCTION(session_start) 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_NOTICE, "Session cannot be started after headers have already been sent (started from %s on line %d)", output_start_filename, output_start_lineno); + php_error_docref(NULL, E_WARNING, "Session cannot be started after headers have already been sent (started from %s on line %d)", output_start_filename, output_start_lineno); } else { - php_error_docref(NULL, E_NOTICE, "Session cannot be started after headers have already been sent"); + 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 index d526d51d867e2..49eb0fba93b41 100644 --- a/ext/session/tests/gh-16372.phpt +++ b/ext/session/tests/gh-16372.phpt @@ -16,4 +16,4 @@ session_start(); --EXPECTF-- Sent headers -Notice: session_start(): Session cannot be started after headers have already been sent (started from %s on line %d) in %s on line %d +Warning: session_start(): Session cannot be started after headers have already been sent (started from %s on line %d) in %s on line %d From fd6ca7398a6328859d055199b32ef694fe247693 Mon Sep 17 00:00:00 2001 From: Calvin Buckley Date: Mon, 14 Oct 2024 13:55:21 -0300 Subject: [PATCH 3/3] make clear this is where headers and not session were sent --- ext/session/session.c | 2 +- ext/session/tests/gh-16372.phpt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/session/session.c b/ext/session/session.c index 020fa19a2e59e..8f0acdbcd892b 100644 --- a/ext/session/session.c +++ b/ext/session/session.c @@ -2648,7 +2648,7 @@ PHP_FUNCTION(session_start) 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 (started from %s on line %d)", output_start_filename, output_start_lineno); + 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"); } diff --git a/ext/session/tests/gh-16372.phpt b/ext/session/tests/gh-16372.phpt index 49eb0fba93b41..7ce572792bc3f 100644 --- a/ext/session/tests/gh-16372.phpt +++ b/ext/session/tests/gh-16372.phpt @@ -16,4 +16,4 @@ session_start(); --EXPECTF-- Sent headers -Warning: session_start(): Session cannot be started after headers have already been sent (started from %s on line %d) in %s on line %d +Warning: session_start(): Session cannot be started after headers have already been sent (sent from %s on line %d) in %s on line %d