PHP header() warning about a new line in the header
Warning: Header may not contain more than a single header, new line detectedShort answer
PHP refuses any header() string that has a carriage return or line feed in the middle. It prints the warning and does not send that header. Either you packed two headers into one call, or a value from a file, form or database brought a line break with it. Send one header per call and clean the value first.
Fix it fast
1Find out which cause you have
Print the exact string you pass to header(), with line breaks made visible. The warning is raised on the line of that header() call.
var_dump($headerString); // or: echo json_encode($headerString);2Match what you see, then jump to its fix
- 1
The string contains \r\n or \n between two header lines, for example Location and Cache-Control in one call.Two headers were put into one header() callFix - 2
The header line looks right in the code but the variable contains text such as a name, URL or message with a new line inside it.A value read from a file, form or database holds a line breakFix - 3
Warning: Header may not contain NUL bytes, raised by the same checkA NUL byte hides in the value next to the line breakFix
- 1
What does the error look like?
Warning: Header may not contain more than a single header, new line detected
Cause 1 of 3
Two headers were put into one header() call
1Check
Search the code for header() calls whose string holds a line break or a double-quoted \n.
grep -rn 'header(.*\\n' src/ public/2Fix
PHP sends one header per call and adds the line ending itself. Split the string into one call per header. To send a second header with the same name, pass false as the replace argument.
header('Cache-Control: no-store');
header('Location: /dashboard');
header('Set-Cookie: a=1; Path=/', false);
header('Set-Cookie: b=2; Path=/', false);3Verify
Reload the page. The warning is gone and the headers appear in the response.
curl -sI http://localhost:8000/ | head -20Confirmed by php.net
Cause 2 of 3
A value read from a file, form or database holds a line break
1Check
Look for a CR or LF byte inside the variable. Only breaks in the middle matter: PHP strips trailing white space from the string before checking it.
var_dump(preg_match('/[\r\n]/', $value) === 1);2Fix
Remove line breaks from the value before building the header. If the value comes from a visitor, treat a line break as bad input and reject it rather than repairing it, because that is how header injection works.
$value = trim($value);
if (preg_match('/[\r\n\0]/', $value)) {
http_response_code(400);
exit('Invalid value');
}
header('X-Request-Label: ' . $value);3Verify
Send a request with a clean value and one with a line break. The first sets the header, the second returns 400.
curl -si 'http://localhost:8000/?label=ok' | head -10Confirmed by raw.githubusercontent.com
Cause 3 of 3
A NUL byte hides in the value next to the line break
1Check
A NUL byte is rejected by the same loop that rejects CR and LF. Look for it in values read from binary-safe sources such as uploaded files.
var_dump(strpos($value, "\0") !== false);2Fix
Strip or reject the NUL byte together with the line breaks, as in the previous cause.
$value = str_replace(["\0", "\r", "\n"], '', $value);3Verify
The warning no longer appears for that input.
php -r 'header("X-A: ok"); echo "done\n";'Confirmed by raw.githubusercontent.com
How do I stop it coming back?
- Call header() once per header line, never with a built-up multi-line string.
- Validate any visitor-supplied value that goes into a header, and reject line breaks.
- Prefer a framework response object so headers are set through one checked method.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix