Brief Introduction to HTTP message header webpage Cache Control and common header commands

Source: Internet
Author: User
Tags http authentication
This article provides a detailed analysis of HTTP message header webpage Cache Control and common header commands. For more information, see

This article provides a detailed analysis of HTTP message header webpage Cache Control and common header commands. For more information, see

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. The function of browser review is divided into the following situations:
(1) open a new window
If the values are private, no-cache, must-revalidate, and virtual host, the server will be accessed again when a new window is opened.
If the max-age value is specified for the Hong Kong server, 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 and access the Hong Kong Space when accessing this network, the access will be repeated each time: 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 = http://www.jb51.net ');
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 = "example.zip "');
Header ('content-Transfer-Encoding: binary ');

// Disable caching the current document:
// Load the file to send: readfile('example.zip ');
// 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 ';

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.