Home  >  Article  >  Backend Development  >  PHP header function tutorial_PHP tutorial

PHP header function tutorial_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:00:33909browse

The header() function sends a raw HTTP header to the client. header(string,replace,http_response_code) It is important to see that header() must be sent before any actual output is called ​

PHP header function tutorial

Definition and usage
The header() function sends a raw HTTP header to the client.

It's important to see that headers ( ) must be sent before any actual output is called (in PHP 4 and later, you can use output buffering to solve this problem):

// This results in an error.
// The output above is before the header() call
header('Location: http://www.example.com/');
?>

Syntax:

header(string,replace,http_response_code)

		Parameter
Description
string 必需的。指定的标题字符串发送
replace 任择。指示是否标题应取代以前或添加第二个标题。预设值是true (将取代) 。假(允许多个标题同一类型)
http_response_code 任择。部队的HTTP响应代码到指定的值(可在PHP 4.3和更高)
Description
string Required. The specified header string is sent
replace Optional. Indicates whether the title should replace the previous one or add a second title. The default value is true (will be replaced). false (allows multiple titles of the same type)
http_response_code Optional. Forces the HTTP response code to a specified value (available in PHP 4.3 and higher)


Tips and Instructions NOTE: As of PHP 4.4 this feature prevents more than one header from being sent at once. This is a protection against header injection attacks. Example 1 Prevent page caching:

// Date in the pastheader("Expires: Mon, 26 Jul 1997 05:00:00 GMT");header("Cache-Control: no-cache");header("Pragma: no-cache") ;

Example 2 Let the user be prompted to save the generated PDF file (the content-disposition header is used to provide suggested filenames and force the browser to show the save dialog):

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

// It will be called downloaded.pdfheader("Content-Disposition:attachment;filename='downloaded.pdf'");
// The PDF source is in original.pdfreadfile("original.pdf");

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445505.htmlTechArticleheader () function sends a raw HTTP header to the client. header(string,replace,http_response_code) It is important to see that header( ) must be sent before any actual output is called...
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