Send a raw HTTP header to the client.
The header() function sends a raw HTTP header to the client. It must be called before any actual output is sent, including blank lines, HTML, or whitespace before the opening <?php tag, otherwise PHP emits a warning and the header is not sent.
Common uses include redirecting users with Location:, setting content types, controlling caching, sending cookies via Set-Cookie:, and emitting custom HTTP status codes. To send a status line, use a string like 'HTTP/1.1 404 Not Found' or pass the response_code parameter.
By default, headers with the same name replace any previously set header. Set the optional replace parameter to false to send multiple headers with the same name (useful for some Set-Cookie or Link relationships). For sending HTTP status codes specifically, http_response_code() is often clearer.
| Name | Description | Optional |
|---|---|---|
header |
The header string to send. | No |
replace |
Whether to replace a previous similar header. | Yes |
response_code |
Forces the HTTP response code to the specified value. | Yes |
// Redirect
header('Location: https://example.com/login');
exit;
// JSON response
header('Content-Type: application/json; charset=utf-8');
echo json_encode(['ok' => true]);
// 404 Not Found
header('HTTP/1.1 404 Not Found');