The Phpheader () function syntax and usage code are described in detail. common status codes of headers are listed here.
Syntax:
The code is as follows:
Void header (string $ string [, bool $ replace = true [, int $ http_response_code)
Send the original HTTP header to the client
Note:
The Header function must be called before any actual output, whether it is a common html tag, a hollow line of a file, or from php. That is, there cannot be any form of output before this function.
Parameter description:
Parameter description
String is required. Specifies the header string to be sent.
Replace is 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 is optional. Forces the HTTP response code to the specified value. (PHP 4 and later versions are available)
Examples of common usage:
I. sending server status code
The code is as follows:
Header ('http/1.0 404 Not Found ');
Common status codes:
Status Code description
100-199 indicates that the request is successfully received, and the client is required to submit the next request to complete the processing.
200-299 successfully received the request and completed the entire process.
300-399 incomplete requests, the client needs to further refine the request, for example, the requested resource has been moved to a new address, usually 302,304
400-499 Client request errors 404
500-599 server errors, commonly used 500
For details, see The HTTP & FTP prompts.
II. page jump
The code is as follows:
// Jump now
Header ('Location: http://www.jb51.net /');
// Jump 3 seconds later
Header ('refresh: 3; url = http://www.jb51.net ');
3. set browser cache
Force the browser not to cache!
[Code]
// Header ('expires:-1 ');
Header ('expires: Mon, 26 Jul 1997 05:00:00 GMT ');
Header ('cache-Control: no-Cache, must-revalidate ');
Header ('pragma: no-cache ');
Header ('last-Modified: '. date ('d, j m y h: I: s t '));
Note:
The time after Expires must be in GMT format, for example: gmdate ("M d y h: I: s ");
Common Cache-control values include private, no-cache, max-age, and must-revalidate. the default value is private. Its function is divided into the following situations based on different re-browsing methods.
Cache-directive description
All public content will be cached
Private content is only cached in private cache
No-cache all content will not be cached
No-store does not cache all content in cache or temporary Internet files.
Must-revalidation/proxy-revalidation if the cached content is invalid, the request must be sent to the server/proxy for re-verification.
Max-age = xxx (xxx is numeric) the cached content will expire after xxx seconds. this option is only available in HTTP 1.1. if it is used together with Last-Modified, high priority
Open new window
If the cache-control values are private, no-cache, and must-revalidate, the server will be accessed again when a new window is opened. If the max-age value is specified, the server will not be accessed again within this value, for example:
The code is as follows:
Header ('cache-control: max-age = 5 ');
Indicates that the server will not be accessed again within 5 seconds after the webpage is accessed.
Press enter in the address bar
If the value is private or Must-revalidate, the server will be accessed only during the first access and will not be accessed in the future. If the value is no-cache, it will be accessed every time. If the value is max-age, the access will not be repeated before expiration.
Press the back button
If the values are private, must-revalidate, and max-age, no re-access will be performed. if the value is no-cache, the access will be repeated each time.
Press refresh
No matter what the value is, it will be accessed again. When the Cache-control value is "no-cache", accessing this page will not leave page backup in the temporary article folder on the Internet. In addition, specifying the "Expires" value also affects the cache. For example, if you specify the Expires value as a time that has passed, if you repeatedly press enter in the address bar when accessing this network, the access will be repeated each time:
Expires: Fri, 31 Dec 1999 16:00:00 GMT
About "Last-Modified
When the browser requests a URL for the first time, the server returns 200, and the content is the resource you requested, at the same time, there is a Last-Modified attribute to mark the Last modification time of this file on the service end. the format is similar to this:
Last-Modified: Fri, 12 May 2006 18:53:33 GMT
When the client requests this URL for the second time, according to the HTTP protocol, the browser will send the If-Modified-Since header to the server, asking If the file has been Modified after the time:
If-Modified-Since: Fri, 12 May 2006 18:53:33 GMT
If the resources on the server are Not Changed, the HTTP 304 (Not Changed.) status code is automatically returned and the content is blank, which saves the amount of data transmitted. When the server code changes or the server is restarted, the resource is re-issued, and the returned result is similar to the first request. This ensures that resources are not repeatedly sent to the client, and that the client can obtain the latest resources when the server changes.
Last-Modified improves performance
Smart developers will use the http header of the Last-Modified and ETags requests together, so that the cache of the client (such as the browser) can be used. Because the server first generates the Last-Modified/Etag mark, the server can use it later to determine whether the page has been Modified. Essentially, the client sends this mark back to the server and requires the server to verify its (client) cache.
The process is as follows:
Client requests A page ()
The server returns page A and adds A Last-Modified
The client displays the page and caches the page together with Last-Modified.
The customer requests page A again and passes the Last-Modified returned by the server in the Last request to the server.
The server checks the Last-Modified and determines that the page has not been Modified since the Last client request. the server returns the 304 response and an empty response body.
III. file download
For example, download a PDF file
The code is as follows:
// We'll be outputting a PDF
Header ('content-type: application/pdf ');
// It will be called downloaded.pdf
// Indicates that it is an attachment, name it, and download it
Header ('content-Disposition: attachment; filename = "downloaded.pdf "');
// The PDF source is in 1.pdf
Readfile('1.pdf ');
In fact, we need to better understand this!
The code is as follows:
Header ('content-type: application/octet-sream ');
Header ('Accept-Ranges: bytes ');
// Return byte
Header ('content-disposition: attachement; filename = "file name "');
Header ('Accept-length: '. $ filesize );