Share via:

PHP: Header

Send HTTP headers using the PHP header function.


Edited: 2019-06-27 12:26

The PHP Header function is used to send HTTP Headers in response to a HTTP request, typically coming from a browser. Headers must be sent before any output is sent to the browser.

To send a simple header response, such as that used for permanent redirects, we may use the below:

header('HTTP/1.1 301 Moved Permanently');

From PHP 5.4 and up, you should use http_response_code() to set response codes, as it is much easier:


Response Codes

Status codes are used to tell the browser if the requested resource exists, has been modified, or if it has been moved to another location.

The standard response for a PHP script is 200 OK, but we can change this using the PHP header function. We would typically do this for caching purposes, or to redirect the request in case the resource was moved. To send a 200 ok message we can do like this:

header('HTTP/1.1 200 OK');

Header Redirect

Redirects are performed with the HTTP Location Header, usually in combination with the relevant status code, such as the 303 See Other status code.

header('HTTP/1.1 303 See Other');

See also

  1. HTTP Response Codes – a list of response codes.