Analysis of HTTP message header webpage cache control and header common instructions _ PHP Tutorial

Source: Internet
Author: User
Tags http authentication
Brief introduction to HTTP message header webpage cache control and common header commands. The Cache of web pages is controlled by "cache-control" in the HTTP message header. common values include private, no-Cache, max-age, and must-revalidate, the default value is private. Its function is controlled by "Cache-control" in the HTTP message header based on the Cache of different web pages, common values include private, no-cache, max-age, and must-revalidate. the default value is private. The function of browser review is divided into the following situations:
(1) open a new window
If the 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 the time range. for example:
Cache-control: max-age = 5 (indicating that the server will not be accessed again within 5 seconds after the webpage is accessed)
(2) 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.
The value is no-cache.
If the value is max-age, the access will not be repeated before expiration.
(3) back and press the button
If the values are private, must-revalidate, and max-age, no re-access is performed,
If the value is no-cache, access is repeated each time.
(4) press the refresh button
No matter what the value is, it will be accessed again
When the Cache-control value is "no-cache", accessing this page does 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 at: Expires: Fri, 31 Dec 1999 16:00:00 GMT
For example, disable page caching in IE.
Http response message header settings:
CacheControl = no-cache
Pragma = no-cache
Expires =-1
Expires is a good stuff. if the webpage on the server changes frequently, set it to-1, indicating that it will expire immediately. If a webpage is updated at every day, you can set Expires to the next day.
When CacheControl = no-cache is specified on the HTTP1.1 server, the browser does not cache the webpage.
The Cache-Control title cannot be used for legacy HTTP 1.0 servers.
To be backward compatible with HTTP 1.0 servers, IE uses the Pragma: no-cache title to provide special support for HTTP.
If the client communicates with the server through a secure connection (https: //) and the server returns the Pragma: no-cache title in the response,
Internet Explorer does not cache this response. Note: Pragma: no-cache prevents caching only when used in secure connections. if used on a non-secure page, the processing method is the same as Expires:-1, this page will be cached but marked as expired immediately.

Common header commands
The header is divided into three parts:
The first part is the HTTP Version );
The second part is the Status code );
The third part is the Reason Phrase (Reason-Phrase ).

The code is as follows:

// Fix 404 pages: Use this header command to solve the 404 header generated by URL rewriting.
Header ('http/1.1 200 OK ');

// Set 404 header: Page not found
Header ('http/1.1 404 Not Found ');

// The page is permanently redirected to notify the search engine to update their urls.
// Set Moved Permanently header (good for redrictions)
// Use with location header
Header ('http/1.1 301 Moved Permanently ');

// Restricted access
Header ('http/1.1 403 Forbidden ');

// Server error
Header ('http/1.1 500 Internal Server Error ');

// Redirect to a new location
// Redirect to a new location:
Header ('Location: );

Redirection after a delay of some time
// Redrict with delay:
Header ('refresh: 10; url = ');
Print 'You will be redirected in 10 seconds ';

// Overwrite X-Powered-By value
// Override X-Powered-By: PHP:
Header ('x-Powered-By: PHP/4.4.0 ');
Header ('x-Powered-By: Brain/0.6b ');

// Content language (en = English)
// Content language (en = English)
Header ('content-language: en ');

// The last modification time (which can be used in cache)
// Last modified (good for caching)
$ Time = time ()-60; // or filemtime ($ fn), etc
Header ('last-Modified: '. gmdate ('d, d m y h: I: S', $ time). 'gmt ');

// Tell the browser that the content to be retrieved has not been updated
// Header for telling the browser that the content
// Did not get changed
Header ('http/1.1 304 Not modified ');

// Set the content length (which can be used for caching ):
// Set content length (good for caching ):
Header ('content-Length: 1234 ');

// Used to download an object:
// Headers for an download:
Header ('content-Type: application/octet-stream ');
Header ('content-Disposition: attachment; filename = " "');
Header ('content-Transfer-Encoding: binary ');

// Disable caching the current document:
// Load the file to send: readfile(' ');
// Disable caching of the current document:
Header ('cache-Control: no-Cache, no-store, max-age = 0, must-revalidate ');
Header ('expires: Mon, 26 Jul 1997 05:00:00 GMT ');

// Set the content type:
// Date in the pastheader ('pragma: no-cache ');
// Set content type:
Header ('content-Type: text/html; charset = ISO-8859-1 ');
Header ('content-Type: text/html; charset = utf-8 ');
// Plain text file
Header ('content-Type: text/plain ');

// JPG picture
Header ('content-Type: image/jpeg ');

// ZIP file
Header ('content-Type: application/zip ');

// PDF file
Header ('content-Type: application/pdf ');

// Audio MPEG (MP3,...) file
Header ('content-Type: audio/mpeg ');

// Flash animation // show sign in box
Header ('content-Type: application/x-shockwave-Flash ');

// The logon dialog box is displayed for HTTP authentication.
Header ('http/1.1 401 unauthorized ');
Header ('www-Authenticate: Basic realm = "Top Secret "');
Print 'text that will be displayed if the user hits cancel or ';
Print 'enters wrong login data ';

Bytes. Its function varies...

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.