PHP Reference ยท Web and Security
PHP header() function
Reference for PHP header(): send raw HTTP headers, redirect with Location, set content types and downloads, avoid 'headers already sent' and header injection.
header() adds a line to the HTTP response that PHP sends to the browser. Headers come before the page body and tell the browser how to treat it: the content type, a redirect target, caching rules, a download filename, security policies and more.
Because headers travel first, header() only works while no output has been sent. The first echo, HTML outside <?php ?> tags, or even a blank line before the opening tag starts the body and locks the headers. After that PHP warns "headers already sent".
header('Content-Type: application/json; charset=utf-8'); header('Cache-Control: no-store'); echo json_encode(['ok' => true]);
Try it
Headers belong to web requests. The editor here runs PHP from the command line, so there is no browser to receive them and headers_list() stays empty. The examples therefore show what you can observe: the "too late" warning, the safety check and a helper that builds header lines. The web-only snippets are read-only and labeled.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Typical web-only uses (read-only, they need a real HTTP request):
// Redirect after a form post header('Location: /thanks', true, 303); exit; // Force a download header('Content-Type: application/pdf'); header('Content-Disposition: attachment; filename="report.pdf"'); readfile($path); // A security header header('X-Content-Type-Options: nosniff');
Syntax
header(string $header, bool $replace = true, int $response_code = 0): void
| Name | Description |
|---|---|
$header |
One complete header line, for example 'Content-Type: text/plain'. A line starting with HTTP/ sets the status line; a Location: header sets the status to 302 unless a 201 or 3xx code is already set. |
$replace |
true (default) replaces an earlier header of the same name; false adds another line, useful for headers that may repeat. |
$response_code |
Optional HTTP status code to send along, for example 301 or 404. Only applied if $header is not empty. |
Return value: none (void).
Errors / Exceptions: no exception. If output has already started, PHP raises a warning ("Cannot modify header information - headers already sent by ...") and ignores the call. A value containing a line break raises a warning and is not sent.
Usage notes
- Check
headers_sent()when code may run after output; its optional by-reference arguments tell you the file and line where output began. - Typical causes of "headers already sent": a space or newline before
<?phpor after a closing?>, a byte-order mark in the file, anechoor a notice printed earlier. Omit the closing tag in pure PHP files, and use output buffering only as a last resort. - Redirect then stop.
header('Location: ...')does not end the script. Callexitright after, or the rest of the page still runs. - Prefer a 303 or 302 after POST, 301 or 308 for permanent moves. Pass the code as the third argument or use
http_response_code(). - Never put raw user input in a header. Modern PHP rejects newlines, but validate redirect targets yourself to avoid open redirects.
header_remove('Name')drops a header you or PHP added, for exampleheader_remove('X-Powered-By').- Header names are case-insensitive, but write them in the usual
Content-Typestyle. - Cookies are headers too, but use
setcookie(), which formats them correctly. - Frameworks wrap this in response objects and send everything at once; the second example shows the idea.