What is header() function in PHP?

WBOY
Release: 2023-09-07 06:00:01
forward
1562 people have browsed it

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');
Copy after login

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');.
Copy after login

Set the HTTP status in the header response.

Example:

header("HTTP/1.0 404 Not Found");
Copy after login

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:< /h2>
header("Cache-Control: no-cache, must-revalidate");
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!