Home  >  Article  >  Backend Development  >  The role of php header

The role of php header

angryTom
angryTomOriginal
2019-08-23 13:26:213709browse

The role of php header

This article mainly introduces you to several major functions of the PHP header function. I hope it can be helpful to you.

Recommended tutorial: PHP video tutorial

First look at the definition of the official document

( PHP 4, PHP 5, PHP 7)

header — Send native HTTP header

void header ( string $string [, bool $replace = true [, int $http_response_code ]] )

Parameters:

string

There are two special heads. 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 a request for a file that does not exist (using the ErrorDocument directive), you will hope that the script responds with the correct status code.

The second special case is the "Location:" header information. It not only sends the message to the browser, but also returns a REDIRECT (302) status code to the browser, unless the status code has been set to 201 or 3xx in advance.

replace

The optional parameter replace indicates whether to replace the previous header of the same type with the later header. Replaced by default. If FALSE is passed in, the same header information can be forced to coexist. For example:

http_response_code

Forcefully specify 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. Specify content:

header('Content-type : application/pdf');

 3. Attachment:

  header('Content-type: 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

The above is the detailed content of The role of php header. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:How to use define in phpNext article:How to use define in php