PHP header ()

Source: Internet
Author: User
Tags response code

PHP header () function

PHP HTTP functions

Definition and usage

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

It is important to recognize that the header () function must be called before any actual output is sent (in PHP 4 and later, you can use output caching to resolve this issue):

header(‘Location: http://www.example.com/‘); ?>
Grammar
Header (String,replace,http_response_code)
Parameters Description
String Necessary. Specifies the string of headers to send.
Replace

Optional. Indicates whether the header replaces the previous header, or adds a second header.

The default is true (replace). False (multiple headers of the same type are allowed).

Http_response_code Optional. Forces the HTTP response code to the specified value. (Available in PHP 4 and later)
Hints and Notes

Note: After PHP 4.4, this function prevents multiple headers from being sent at once. This is a protective measure against head-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"); ? >

Note: The user may set some options to change the browser's default cache settings. By sending the above header, you can override any of these settings to force the browser not to cache!

Example 2

Prompts the user to save a generated PDF file (the content-disposition header is used to provide a recommended file name and to force the browser to display the Save dialog box):

<?php header("Content-type:application/pdf"); //file will be called Downloaded.pdf header("Content-Disposition:attachment;filename=‘downloaded.pdf‘"); //pdf Source in Original.pdf ReadFile ("original.pdf");? >

Note: Microsoft IE 5.5 has a bug that prevents the above mechanism. You can resolve the bug by upgrading to a version of Service Pack 2 or higher.

PHP header ()

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.