Home >Backend Development >PHP Tutorial >What is header() function in PHP?

What is header() function in PHP?

WBOY
WBOYforward
2023-09-06 23:05:111642browse

What is header() function in PHP?

The header() function is a predefined PHP native function. The header() HTTP function allows us to control the data that the web server sends to the client or browser before sending other output.

The header function sets the headers of the HTTP response given by the server. We can use the header function in PHP to do various things, such as changing the page location, setting the time zone, setting cache control, etc...

Listed some important uses of header() in PHP as follows :

Redirect page.

Used to redirect one web page to another web page in PHP.

Example:

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

Set Content-Type in header response:

PHP sends Content-Type: text/html by default. If we want to change the Content-Type, we can use the header() function to do so.

Example:

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

Set the HTTP status in the header response.

Example:

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

Send the response to the browser without caching.

The following example helps prevent caching by sending header information that overrides the browser's settings to not cache.

Example:
header("Cache-Control: no-cache, must-revalidate");

The above is the detailed content of What is header() function in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete