What is header() function in PHP?

PHPServer Side ProgrammingProgramming

The header() function is an predefined PHP native function.With header() HTTP functions we can control data sent to the client or browser by the Web server before some other output has been sent.

The header function sets the headers for an HTTP Response given by the server. We can do all sorts of things using the header function in PHP like Change page location, set timezone, set caching control, etc...

Some of the important uses of the header() in PHP are listed below:

Redirect page.

It is used to redirect a from one web page to another web page in PHP.


header('Location:give your url here');

Set Content-Type in header response:

PHP defaults to sending Content-Type:text/html.If we want to change the Content-Type, we can achieve that with the header() function.


Generated PDF file :header('Content-Type: application/pdf');
Return response in json format:header('Content-Type: application/pdf');.

Set HTTP Status in the header response.


header("HTTP/1.0 404 Not Found");

Sent the response to a browser with no cache.

The below example helps to prevent caching by sending header information which overrides browser setting to not-cache.


header("Cache-Control: no-cache, must-revalidate");
Updated on 30-Jul-2019 22:30:26