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: http://www.xker.com /");
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
Syntax:
Header (string, replace, http_response_code)
Parameters
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
- <? PHP
- Header ("Location: http://www.example.com /");
- Exit;
- ?>
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.
- <? PHP
- // Tell the browser the expiration time of this page (expressed by Greenwich Mean Time), as long as it is a date that has passed.
- Header ("expires: Mon, 26 Jul 1970 05:00:00 GMT ");
- // 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
- Header ("last-modified:". gmdate ("D, D m y h: I: s"). "GMT ");
- // Tell the client browser not to use the cache
- Header ("cache-control: No-cache, must-revalidate ");
- // Parameter (compatible with the previous server), that is, compatible with http1.0 Protocol
- Header ("Pragma: No-Cache ");
- ?>
Example 3:
Outputs the status value to the browser, which is mainly used for access permission control.
- <? PHP
- Header ('HTTP/1.1 401 unauthorized ');
- Header ('status: 401 unauthorized ');
- ?>
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.
- <? PHP
- Header ('HTTP/1.1 404 Not found ');
- Header ("status: 404 Not Found"); // This write method is incorrect
- ?>
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:
- <? PHP
- // Some code here
- ?>
- // It should be a blank line
- <? PHP
- Header ("HTTP/1.1 403 Forbidden ");
- Exit ();
- ?>
cause:
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.
solution:
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.