Skip to content

Commit edf351c

Browse files
authored
Mention where headers were already sent if session_start fails (php#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 phpGH-16372
1 parent 275c7f2 commit edf351c

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

ext/session/session.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -2654,7 +2654,14 @@ PHP_FUNCTION(session_start)
26542654
* module is unable to rewrite output.
26552655
*/
26562656
if (PS(use_cookies) && SG(headers_sent)) {
2657-
php_error_docref(NULL, E_WARNING, "Session cannot be started after headers have already been sent");
2657+
/* It's the header sent to blame, not the session in this case */
2658+
const char *output_start_filename = php_output_get_start_filename();
2659+
int output_start_lineno = php_output_get_start_lineno();
2660+
if (output_start_filename != NULL) {
2661+
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);
2662+
} else {
2663+
php_error_docref(NULL, E_WARNING, "Session cannot be started after headers have already been sent");
2664+
}
26582665
RETURN_FALSE;
26592666
}
26602667

ext/session/tests/gh-16372.phpt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
GH-16372: Mention where headers were already sent if session_start fails
3+
--EXTENSIONS--
4+
session
5+
--SKIPIF--
6+
<?php include('skipif.inc'); ?>
7+
--FILE--
8+
<?php
9+
10+
header("X-PHP-Test: test");
11+
12+
echo "Sent headers\n";
13+
14+
session_start();
15+
?>
16+
--EXPECTF--
17+
Sent headers
18+
19+
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 commit comments

Comments
 (0)