PHP header function tutorial

Source: Internet
Author: User

The header () function sends an original HTTP header to the client. Header (string, replace, http_response_code) It is important to see that the title () must be sent in any so-called actual output

PHP header function tutorial

Definition and usage
The header () function sends an original HTTP header to the client.

It is important to see that the title () must be sent in any so-called actual output (after PHP 4 and later, you can use the output buffer 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 Required. The specified title string is sent.
Replace Optional. Indicates whether the title should replace the previous or add the second title. The default value is true (will be replaced ). False (multiple titles of the same type are allowed)
Http_response_code Optional. The HTTP response code of the troops to the specified value (available in PHP 4.3 and higher)
 

Note: The PHP 4.4 feature prevents more than one title from being sent 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 ask the user to save the generated PDF file (the Content Disposition title is used to provide a suggested file name and force the browser to display the Save dialog box ):

 

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");

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.