By using this site you agree to the use of cookies.

Learn more

Share via:

PHP: Header

Send HTTP headers using the PHP header function.

Edited: 2017-06-19 14: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” );
Header( “Location: http://beamtic.com/new-url” );

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');
header('Location: http://www.example.com/');

See also

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