PHP header function tutorial

Source: Internet
Author: User
Tags response code

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 ):

<? Php
// 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)
  

Tips and instructions
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.pdf header ("Content-Disposition: attachment?filename='downloaded= '");
// The PDF source is in original.pdf readfile ("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.