Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP header() warning about a new line in the header

ErrorWarning: Header may not contain more than a single header, new line detected

Short 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.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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);
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The 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. 3Warning: Header may not contain NUL bytes, raised by the same checkA NUL byte hides in the value next to the line breakFix

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

Confirmed by php.net

Did this fix it?

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

Confirmed by raw.githubusercontent.com

Did this fix it?

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

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.