Current Location: Home> Function Categories> header

header

Send native HTTP header
Name:header
Category:Network
Programming Language:php
One-line Description:Sends the original HTTP header to the client.

Definition and usage

header() function is used to send the original HTTP header to the client.

It is important to note that the header() function must be called before any actual output!

Example

Example 1

Send three HTTP headers to prevent page caching:

 <?php
// Past date
header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ) ;
header ( "Cache-Control: no-cache" ) ;
header ( "Pragma: no-cache" ) ;
?>

< html >
< body >
...
...

Note: Users may set some options to change the browser's default cache settings. By sending the above headers, you will override these settings and force the browser to not cache!

Example 2

Prompt the user to save the generated PDF file (using the Content-Disposition header to provide a recommended file name and force the browser to display the save dialog):

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

// It will be named downloaded.pdf
header ( "Content-Disposition:attachment;filename='downloaded.pdf'" ) ;

// PDF source file is in original.pdf
readfile ( "original.pdf" ) ;
?>

< html >
< body >

...
...

grammar

 header ( header , replace , http_response_code )

Parameter value

parameter describe
header Required. Specifies the header string to be sent.
replace

Optional. Indicates whether the header should replace the previous similar header or add a new header of the same type.

The default value is TRUE (will replace). FALSE allows multiple headers of the same type.

http_response_code Optional. Forces the HTTP response code to the specified value.