What does the php header function do? In php, the header function can be used as two special headers to send status codes. It can replace the previous header of the same type, or it can force the value of the HTTP response to be specified; then, let's take a look at the specific content.
Let’s take a look at the definition in the official document first
(PHP 4, PHP 5, PHP 7)
header —Send native HTTP header
1 void header ( string $string [, bool $replace = true [, int $http_response_code ]] )
Parameters:
string
There are two special head of. The first one starting with "HTTP/" (case is not significant) will be used to calculate the HTTP status code to be sent. For example, if you use a PHP script on the Apache server to handle requests for files that do not exist (using theErrorDocumentdirective), you will hope that the script responds with the correct status code.
1
The second special case is the "Location:" header information. It not only sends the message to the browser, but also returns aREDIRECT(302) status code to the browser, unless the status code has been set to201or ## in advance. #3xx.
1
replace
Optional parameterreplaceIndicates whether to use the following The header replaces the previous header of the same type. Replaced by default. If you passFALSE
, you can force the same header information to coexist. For example:
1
http_response_code
Forces the value of the HTTP response. Note that this parameter is only valid when the message string (string) is not empty.
The common uses of the header function are as follows:
1. Redirection
header('Location: http://www.example.com/');
2. Specified content:
header ('Content-type: application/pdf'); : application/pdf');
//Specify the content as an attachment and specify the name for download display
header('Content -Disposition: attachment; filename="downloaded.pdf"');## //Open the file and output
readfile('original .pdf');
The above code can produce the effect of a file dialog box in the browser
4. Allow users to obtain the latest information and Data instead of caching
Header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Set critical time
Detailed example:
Detailed explanation of php header function
Usage of PHP header() function
The above is the detailed content of What does the PHP header function do? Introduction to PHP header function (with code). For more information, please follow other related articles on the PHP Chinese website!