Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of HTTP header Web Page Cache Control and header Common instructions

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the HTTP message header web page cache control and header commonly used instruction example analysis, the article is very detailed, has a certain reference value, interested friends must read it!

The cache of web pages is controlled by the "Cache-control" in the HTTP message header. The common values are private, no-cache, max-age, must-revalidate and so on. The default is private. Its function can be divided into the following situations according to different ways of re-browsing:

(1) Open a new window

If the values are private, no-cache, must-revalidate, then the server will be revisited when a new window is opened for access.

If the max-age value is specified, the server will not be reaccessed within this value, for example:

Cache-control: max-age=5 (means that if you visit this page again within 5 seconds, you will not go to the server)

(2) enter in the address bar

A value of private or must-revalidate accesses the server only the first time it is accessed, and will not be accessed later.

If the value is no-cache, it will be accessed every time.

If the value is max-age, the access is not repeated until it expires.

(3) Press the back button

If the values are private, must-revalidate, max-age, the access will not be revisited

If the value is no-cache, the access is repeated every time

(4) Press the refresh button

No matter what the value is, the visit will be repeated.

When the Cache-control value is "no-cache", accessing this page does not leave a page backup in the Internet temporary article folder.

In addition, caching is also affected by specifying a "Expires" value. For example, if you specify the expires value as a time that has already passed, if you repeatedly press enter in the address bar when accessing this network, the visit will be repeated each time: Expires: Fri, 31 Dec 1999 16:00:00 GMT

For example: prevent pages from being cached in IE

Http response message header settings:

CacheControl = no-cache

Pragma=no-cache

Expires =-1

Expires is a good thing. If the web page on the server changes frequently, set it to-1, which means that it expires immediately. If a web page is updated at 1: 00 a. M. every day, Expires can be set to 1: 00 am the next day.

When the HTTP1.1 server specifies CacheControl = no-cache, the browser does not cache the page.

Legacy HTTP 1.0 servers cannot use Cache-Control headers.

So for backward compatibility with HTTP 1.0 servers, IE uses Pragma:no-cache headers to provide special support for HTTP.

If the client communicates with the server through a secure connection (https://)/, and the server returns a Pragma:no-cache header in the response

Then Internet Explorer does not cache this response. Note: Pragma:no-cache prevents caching only when used in secure connections, and if used in non-secure pages in the same way as Expires:-1, the page will be cached but marked as immediately expired.

Header common instructions

Header is divided into three parts:

The first part is the version of HTTP (HTTP-Version).

The second part is the status code (Status)

The third part is the reason phrase (Reason-Phrase).

The copy code is as follows:

/ / fix 404 pages: use this header instruction to solve the 404 header generated by URL rewriting

Header ('HTTP/1.1 200OK')

/ / set 404 header: page not found

Header ('HTTP/1.1 404 Not Found')

/ / pages are permanently redirected, and you can tell search engines to update their urls

/ / set Moved Permanently header (good for redrictions)

/ / use with location header

Header ('HTTP/1.1 301 Moved Permanently')

/ / access is restricted

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: http://www.www.jb51.net);

Redirect after a period of delay

/ / redrict with delay:

Header ('Refresh: 10; url= https://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')

/ / Last modification time (can be used when caching)

/ / last modified (good for caching)

$time = time ()-60; / / or filemtime ($fn), etc

Header ('Last-Modified:' .gmdate ('D, d M Y Hpuri time, $gmdate). GMT')

/ / tell the browser that the content to be obtained has not been updated

/ / header for telling the browser that the content

/ / did not get changed

Header ('HTTP/1.1 304Not Modified')

/ / set the length of the content (can be used when caching):

/ / set content length (good for caching):

Header ('Content-Length: 1234')

/ / used to download files:

/ / Headers for an download:

Header ('Content-Type: application/octet-stream')

Header ('Content-Disposition: attachment; filename= "example.zip"')

Header ('Content-Transfer-Encoding: binary')

/ / disable caching of 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')

/ / displays the login dialog box, which can be used 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'

The above is all the contents of the article "HTTP header web cache control and sample analysis of header common instructions". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report