Function header of PHP

Source: Internet
Author: User

HTTP header information
Header information has many functions, mainly including the following:
1. Jump
When the browser accepts location: xxxx in the header information, it will automatically jump to the URL address pointed to by XXXX, which is similar to writing a jump using Js. But this jump is only known by the browser, no matter whether there is anything in the body, the user can not see it.
Example: Header ("Location: /");
2. Specify the webpage content
If Content-Type: Application/XML is specified in the header of an XML file, the browser parses the file according to the XML file format. However, if the header is Content-Type: text/XML, the browser will regard it as saved text parsing. (The browser does not parse the file according to the extension)
Example: Header ('content-type: Application/pdf ');
3. Attachment
I don't know if you have noticed that sometimes some websites Download Something. After the link is downloaded, the browser opens the attachment as a webpage and all the garbled characters are displayed. This problem is also related to the header information. Sometimes the browser determines whether to open or save the content based on the Content-Type, and sometimes determines the error (mainly because the website designer forgets to write the Content-Type ). In fact, you can specify the content as an attachment and need to save it. This is: Content-Disposition: attachment; filename = "XXXXX"
Example: Header ('content-Disposition: attachment; filename = "downloaded.pdf "');
// Open the file and Output
Readfile('original ');
HTTP status code
1 × Reserved
2×× indicates that the request is successfully received.
3×× further refine the request to complete the request
4×× customer Error
5×× Server Error
Header (string, replace, http_response_code)
String: required. Specifies the header string to be sent.
Replace: 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: Optional. Send HTTP Response Code It must be specified. (PHP 4 and later versions are available)
Example 1:
Redirect webpage

    1. <? PHP
    2. Header ("Location: /");
    3. Exit;
    4. ?>

Example 2:
Force users to obtain the latest information each time they access this page, instead of using the cache that exists on the client.

    1. <? PHP
    2. // Tell the browser the expiration time of this page (expressed by Greenwich Mean Time), as long as it is a date that has passed.
    3. Header ("expires: Mon, 26 Jul 1970 05:00:00 GMT ");
    4. // Tell the browser the last update date (expressed by Greenwich Mean Time) of this page, that is, the day, to force the browser to obtain the latest information
    5. Header ("last-modified:". gmdate ("D, D m y h: I: s"). "GMT ");
    6. // Tell the client browser not to use the cache
    7. Header ("cache-control: No-cache, must-revalidate ");
    8. // Parameter (compatible with the previous server), that is, compatible with http1.0 Protocol
    9. Header ("Pragma: No-Cache ");
    10. ?>

Example 3:
Outputs the status value to the browser, which is mainly used for access permission control.

    1. <? PHP
    2. Header ('HTTP/1.1 401 unauthorized ');
    3. Header ('status: 401 unauthorized ');
    4. ?>

Example 4:
To restrict a user from accessing the page, you can set the status to 404, as shown below, so that the browser shows that the page does not exist.

    1. <? PHP
    2. Header ('HTTP/1.1 404 Not found ');
    3. Header ("status: 404 Not Found"); // This write method is incorrect
    4. ?>

Example 5:
Hide File Location
The HTML tag <a href = 'HTTP: // hostname/*. mp3'> </a> allows you to download common files. To keep files confidential, you can use the header function to download files.

Header ("Content-Type: Audio/MPEG ");
Header ("content-Disposition: attachment; filename = filenale ");
Header ("content-Description: php3 generated data ");

Example 6:
Input content before the header Function
Generally, HTML content cannot be output before the header function. For example, there are setcookie () and session functions. These functions need to add message header information to the output stream. If an echo statement exists before the header () is executed, When Header () is encountered later, "Warning: cannot modify header information-headers already sent .... "error. That is to say, there cannot be any text, blank lines, and carriage return before these functions, and it is best to add the exit () function after the header () function. For example, the following error code contains an empty line between two PHP code segments:

    1. <? PHP
    2. // Some code here
    3. ?>
    4. // It should be a blank line
    5. <? PHP
    6. Header ("HTTP/1.1 403 Forbidden ");
    7. Exit ();
    8. ?>

when the PHP script starts execution, it can send both the HTTP message header (title) information and the subject information. the HTTP message header (From header () or setcookie () function) is not sent immediately. Instead, it is saved to a list. this allows you to modify the title information, including the default title (for example, Content-Type title ). however, once the script sends any non-title output (for example, called using HTML or print (), PHP must first send all headers and then terminate httpheader. then send the subject data. at this time, any attempt to add or modify the header information is not allowed, and one of the above error messages will be sent.
modify PHP. INI open cache (output_buffering ), change output_buffering = 0 to output_buffering = 4096
or use the cache functions ob_start () and ob_end_flush () in the Program . Principle: When output_buffering is enabled, PHP does not send httpheader when the script sends the output. Instead, it inputs the output to the dynamically added cache through the pipeline (PIPE) (which can only be used in php4.0 and has a centralized output mechanism ). You can still modify/Add the header or set the cookie because the header is not actually sent. When all scripts are terminated, PHP automatically sends the HTTP header to the browser, and then sends the content in the output buffer.

Related Article

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