Home >Backend Development >PHP Tutorial >Introduction to PHP functions: header() function
Introduction to PHP functions: header() function, which implements web page jumps and sets HTTP response headers
In PHP, the header() function is a very important function , it can not only jump to web pages, but also set HTTP response header information. This article will introduce the use of header() function in detail and provide specific code examples.
The basic syntax of the header() function is as follows:
header(string $header, bool $replace = true, int $http_response_code = 0): bool
The following are common application scenarios and specific code examples of the header() function:
header("Location: http://www.example.com"); exit;
header("Content-Type: application/json");
header("HTTP/1.1 404 Not Found");
header("Cache-Control: no-cache, no-store, must-revalidate"); header("Pragma: no-cache"); header("Expires: 0");
header("Content-Disposition: attachment; filename=example.pdf"); header("Content-Type: application/pdf"); header("Content-Length: " . filesize("example.pdf")); readfile("example.pdf");
Summary: The
header() function is a very important PHP function, which can realize web page jumps and set HTTP response headers and other functions . Its flexibility allows us to flexibly adjust HTTP header information according to needs. We should be familiar with how to use the header() function and use it reasonably to achieve the functions we need.
Please note that the header() function must be called before all output, otherwise an error will be reported. After calling the header() function, in order to avoid unexpected situations, we recommend using exit to terminate script execution immediately.
I hope that through the introduction of this article, readers can fully understand the usage of the header() function and be able to use it flexibly in actual projects.
The above is the detailed content of Introduction to PHP functions: header() function. For more information, please follow other related articles on the PHP Chinese website!