Usage of the header function in php

Source: Internet
Author: User
Tags php tutorial response code versions

The header () function sends the original HTTP header to the client.

It is important to realize that the header () function must be called before any actual output is sent (in PHP 4 and later versions, you can use the output cache to solve this problem):

<Html>
<? Php Tutorial
// Result error
// Output already exists before header () is called
Header ('Location: http://www.example.com /');
?> Syntax
Header (string, replace, http_response_code) parameter description
String is required. Specifies the header string to be sent.
Replace is optional. Indicates whether the header replaces the previous header or adds the second header.

The default value is true (replace ). False (multiple headers of the same type are allowed ).
 
Http_response_code is optional. Forces the HTTP response code to the specified value. (PHP 4 and later versions are available)
Tips and comments
Note: After PHP 4.4, this function prevents multiple headers from being sent at a time. This is a protection against header injection attacks.
Example
Example 1
<? Php
// Date in the past
Header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT ");
Header ("Cache-Control: no-cache ");
Header ("Pragma: no-cache ");
?>

<Html>
<Body>

...
... Note: You may set some options to change the default cache settings of the browser. By sending the above header, you can overwrite any of these settings and force the browser not to cache them!

Example 2
You are prompted to save a generated PDF file (the Content-Disposition header is used to provide a recommended file name and force the browser to display the save dialog box ):

<? Php
Header ("Content-type: application/pdf ");

// The file will be called downloaded.pdf
Header ("Content-Disposition: attachment?filename='downloaded= '");

// The PDF source is in original.pdf
Readfile ("original.pdf ");
?>

<Html>
<Body>

...
...


<? Php
// Result error
// Output already exists before header () is called
Header ('Location: http://www.111cn.net /');
?> Syntax
Header (string, replace, http_response_code)

This function prevents multiple headers from being sent at a time.

<? Php
// Date in the past
Header ("expires: mon, 26 jul 1997 05:00:00 gmt ");
Header ("cache-control: no-cache ");
Header ("pragma: no-cache ");
?>

You are prompted to save a generated pdf file (the content-disposition header is used to provide a recommended file name and force the browser to display the save dialog box ):

When the time exceeds the specified time, the webpage content is invalid. The format is as follows:

Expires = "expires:" http-date

For example: header ("expires: fri, 31 oct 2003 18:00:00 gmt"); indicates on Friday, January 1, October 31, 2003.

Header ("expires: wed, 30 jun 2004 09:00:00 gmt"); indicates Wednesday, June 30, 2004.

Last-modified = "last-modified" ":" http-date

For example, header ("last-modified: wed, 01 jan 2003 12:00:00 gmt"); last Modification time:, January 1, January 1, 2003

Cache control

No-cache allows the server to transmit data to remote users without being saved.

No-store prevents poor versions or sensitive information from being retained.

Must-revalidate indicates that information must be re-evaluated. If the temporarily saved data (cached) is invalid, the server is connected to the user.


For more details, see: http://www.111cn.net/phper/php-function/36348.htm

Let's take a look at this for other types

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.