`
xutao5641745
  • 浏览: 223521 次
  • 性别: Icon_minigender_1
  • 来自: 汨罗
社区版块
存档分类
最新评论

Cache-control使用:header('Cache-control:private')

 
阅读更多

 

Cache-control使用:header('Cache-control:private') 引用 写道
网页的缓存是由 HTTP消息头中的“Cache-control”来控制的,常见的取值有private、no-cache、max-age、must- revalidate等,默认为private。其作用根据不同的重新浏览方式分为以下几种情况:
(1) 打开新窗口
值为private、no-cache、must-revalidate,那么打开新窗口访问时都会重新访问服务器。
而如果指定了max-age值,那么在此值内的时间里就不会重新访问服务器,例如:
Cache-control: max-age=5(表示当访问此网页后的5秒内再次访问不会去服务器)
(2) 在地址栏回车
值为private或must-revalidate则只有第一次访问时会访问服务器,以后就不再访问。
值为no-cache,那么每次都会访问。
值为max-age,则在过期之前不会重复访问。
(3) 按后退按扭
值为private、must-revalidate、max-age,则不会重访问,
值为no-cache,则每次都重复访问
(4) 按刷新按扭
  无论为何值,都会重复访问
Cache-control值为“no-cache”时,访问此页面不会在Internet临时文章夹留下页面备份。
另外,通过指定“Expires”值也会影响到缓存。例如,指定Expires值为一个早已过去的时间,那么访问此网时若重复在地址栏按回车,那么每次都会重复访问: Expires: Fri, 31 Dec 1999 16:00:00 GMT
比如:禁止页面在IE中缓存
http响应消息头部设置:
CacheControl = no-cache
Pragma=no-cache
Expires = -1
Expires是个好东东,如果服务器上的网页经常变化,就把它设置为-1,表示立即过期。如果一个网页每天凌晨1点更新,可以把Expires设置为第二天的凌晨1点。
当HTTP1.1服务器指定 CacheControl = no-cache时,浏览器就不会缓存该网页。
旧式 HTTP 1.0 服务器不能使用 Cache-Control 标题。
所以为了向后兼容 HTTP 1.0 服务器,IE使用Pragma:no-cache 标题对 HTTP 提供特殊支持。
如果客户端通过安全连接 (https://)/与服务器通讯,且服务器在响应中返回 Pragma:no-cache 标题,
则 Internet Explorer不会缓存此响应。注意:Pragma:no-cache 仅当在安全连接中使用时才防止缓存,如果在非安全页中使用,处理方式与 Expires:-1相同,该页将被缓存,但被标记为立即过期
header常用指令
header分为三部分:
第一部分为HTTP协议的版本(HTTP-Version);
第二部分为状态代码(Status);
第三部分为原因短语(Reason-Phrase)。
// fix 404 pages: 用这个header指令来解决URL重写产生的404 header
header('HTTP/1.1 200 OK');

// set 404 header: 页面没找到
header('HTTP/1.1 404 Not Found');

// 页面被永久删除,可以告诉搜索引擎更新它们的urls
// set Moved Permanently header (good for redrictions)
// use with location header
header('HTTP/1.1 301 Moved Permanently');
// 访问受限
header('HTTP/1.1 403 Forbidden');
// 服务器错误
header('HTTP/1.1 500 Internal Server Error');

// 重定向到一个新的位置
// redirect to a new location:
header('Location: http://www.m-bang.com);

延迟一段时间后重定向
// redrict with delay:
header('Refresh: 10; url=http://www.sina.com.cn');
print 'You will be redirected in 10 seconds';

// 覆盖 X-Powered-By value
// override X-Powered-By: PHP:
header('X-Powered-By: PHP/4.4.0');
header('X-Powered-By: Brain/0.6b');

// 内容语言 (en = English)
// content language (en = English)
header('Content-language: en');

//最后修改时间 (在缓存的时候可以用到)
// 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');

// 告诉浏览器要获取的内容还没有更新
// header for telling the browser that the content
// did not get changed
header('HTTP/1.1 304 Not Modified');

// 设置内容的长度 (缓存的时候可以用到):
// set content length (good for caching):
header('Content-Length: 1234');

// 用来下载文件:
// Headers for an download:
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.zip"');
header('Content-Transfer-Encoding: binary');

// 禁止缓存当前文档:
// 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');
// 设置内容类型:
// 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');
header('Content-Type: text/plain');

// plain text file
header('Content-Type: image/jpeg');

// JPG picture
header('Content-Type: application/zip');

// ZIP file
header('Content-Type: application/pdf');

// PDF file
header('Content-Type: audio/mpeg');

// Audio MPEG (MP3,...) file
header('Content-Type: application/x-shockwave-flash');

// 显示登录对话框,可以用来进行HTTP认证
// Flash animation// show sign in box
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';
?>
现在表单的填写,我们可以用AJAX对用户随时进行验证,进行友好的提示,但是在用户没有留意AJAX友好提示,提交了错误的表单,跳回原页,而填写的信息却全部丢失了。要支持页面回跳,有以下的办法:
1.使用session_cache_limiter方法: session_cache_limiter('private,must-revalidate');但是要值得注意的是 session_cache_limiter()方法要写在session_start()方法之前才有用;
2.用header来设置控制缓存的方法: header('Cache-control:private,must-revalidate');

 

原文来自:http://zhangxiaofei576342.blog.163.com/blog/static/2086199020101026113241602/

 

 

  

   Cache-Control标头和OutputCache

HTTP 协议通过在头部添加 Cache-Control 标头来控制页面的缓存动作。HTTP 1.1 协议中的 Cache-Control 标头有以下设置:

public
数据内容皆被储存起来,就连有密码保护的网页也储存,安全性很低。

private
数据内容只能被储存到私有的缓存中,通常是浏览器的本地缓存。这是 Cache-Control 标头的默认值。

no-cache
数据内容永远不会被储存,代理服务器和浏览器读到此标头就不会将数据内容存入缓存。

no-store
数据内容除不存入缓存中,也不能存入暂时的磁盘中,这个标头防止敏感性数据被复制。

must-revalidate
用户在每次读取数据时,会再次和原来的服务器确定是否为最新数据,而不是和中间的代理服务器。

proxy-revalidate
这个参数很像 must-revalidate,不过中间接收的代理服务器可以互相分享缓存。

max-age=xxx
数据内容在 xxx 秒后失效,这个标头就像 Exires 标头(HTTP 1.0 协议定义的缓存过期机制)的功能一样,不过 max-age=xxx 只能服务于支持 HTTP 1.1 协议的浏览器。假设两者都有,max-age 优先。

通过 HTTP 协议控制页面的缓存动作,通常可以像如下示例一样添加 Cache-Control 标头在响应流的头部:

HTTP 标头
说明

Cache-Control:max-age=3600
指示页面应在 60 分钟后过期,内容不应在代理服务器上缓存。

Cache-Control:no-cache
指示页面应每次向原来的服务器请求并获取内容。

Cache-Control:public,max-age=3600
指示页面应在 60 分钟后过期,内容可以在浏览器的本地区域和代理服务器上缓存。

但是通过 Cache-Control 标头的控制作用还取决于用户不同的浏览方式:

(1)打开新窗口:如果 Cache-Control 的值为 private、no-cache、must-revalidate,那么打开新窗口访问时都会重新访问服务器。而如果指定了 max-age=xxx 值,则在 xxx 秒时间内都不会重新访问服务器。

(2)地址栏回车:如果 Cache-Control 的值为 private 或 must-revalidate,则只有第一次访问时会访问服务器,以后就不再访问。如果值为 no-cache,那么每次都会访问服务器。如果指定了 max-age,则在过期之前不会访问服务器。

(3)按后退按钮:如果 Cache-Control 的值为 private、must-revalidate、max-age,则不会重新访问服务器。如果值为 no-cache,则每次都重新访问服务器。

(4)按刷新按钮:无论 Cache-Control 为何值,都会重新访问服务器。

通过 Cache-Control 标头设置了缓存的页面,再次向服务器请求时(比如按刷新按钮),浏览器发出的请求头部会添加 If-Modified-Since 标头,该标头向服务器询问从指定的日期后该页面是否已经修改。对于 ASP.NET 而言,根据页面是否开启了服务器端缓存,响应这样的请求会有两种结果,一种是该页面开启了服务器端缓存并且该页面缓存还未过期,则返回 304 的响应,这时浏览器使用本地缓存向用户呈现页面;其它情况则是重新处理该请求并启动新的 ASP.NET 页面生命周期,返回 200 的响应和新的页面内容。注意,请区别这里返回 304 响应的情况和前述的不会重新访问服务器的情况的不同,后者根本就不向服务器发出请求。

话题说到这里,我们已经把基于 HTTP 协议实现的页面缓存进行了说明,但 ASP.NET 同样也给我们提供了服务器端的缓存,这种缓存前面多次提到。为了说明这点,我们先把 HTTP 协议实现的缓存放到一边,就是说我们现在使用了 Cache-Control:no-cache 标头来进行响应。当请求被 IIS 收到时,ASP.NET 会根据页面的缓存设置来决定如何响应,如果页面没有设置服务器缓存,则接下来的事情大家都是知道的;如果页面设置了服务器缓存并且缓存没有过期,则返回 200 的响应和从服务器缓存中存储的页面内容;如果页面设置了服务器缓存但缓存已经过期,则为该页面启动新的 ASP.NET 页面生命周期进行处理,然后缓存新的页面处理内容并响应到客户端。

使用 ASP.NET 开发应用程序,我们完全可以结合由 HTTP 协议实现的缓存和 ASP.NET 自身提供的缓存,具体的应用当然就得看实际的情况了。

关于 @ OutputCache 指令

对于 @ OutputCache 指令已经介绍了 Duration 和 VaryByParam 属性,与“页面输出缓存的本质”小节有关的属性还有 Location 和 NoStore 属性。NoStore 属性接受一个布尔值,如果指定为 true,则向 Cache-Control 标头添加 no-store 设置。Location 属性接受 OutputCacheLocation 枚举值之一,用于控制资源的输出缓存 HTTP 响应的位置。

成员名称
说明

Any
输出缓存可位于产生请求的浏览器客户端、参与请求的代理服务器(或任何其它服务器)或处理请求的服务器上。此值对应于 Cache-Control:public,并开启服务器端缓存。

Client
输出缓存位于产生请求的浏览器客户端上。此值对应于 Cache-Control:private,并关闭服务器端缓存。

Downstream
输出缓存可存储在任何 HTTP 1.1 可缓存设备中,源服务器除外,这包括代理服务器和发出请求的客户端。此值对应于 Cache-Control:public,并关闭服务器端缓存。

Server
输出缓存位于处理请求的 Web 服务器上。此值对应于 Cache-Control:no-cache,并开启服务器端缓存。

None
对于请求的页面,禁用输出缓存。此值对应于 Cache-Control:no-cache,并关闭服务器端缓存,这与在 ASPX 页面上不包含 @ OutputCache 指令没区别。

ServerAndClient
输出缓存只能存储在源服务器或发出请求的客户端中,代理服务器不能缓存响应。此值对于 Cache-Control:private,并开启服务器端缓存。

现在针对 @ OutputCache 指令的基本设置已经介绍完成了,有关它的更多控制细节将在下一讲中阐述,现在我们已经可以与“页面输出缓存的本质”小节中所讲述的内容结合起来,有效的控制页面输出缓存了,如果仅是应付一般的情况,到这样就基本足够了。

 

  

 原文出自:http://cnn237111.blog.51cto.com/2359144/590428

 

 

分享到:
评论

相关推荐

    Cache-control使用Cache-control:private学习笔记

    而如果指定了max-age值,那么在此值内的时间里就不会重新访问服务器,例如:Cache-control: max-age=5(表示当访问此网页后的5秒内再次访问不会去服务器) (2)、在地址栏回车值为private或must-revalidate则只有第一...

    用PHP程序实现支持页面后退的两种方法

    第一,使用Header方法设置消息头Cache-control QUOTE: header(‘Cache-control: private, must-revalidate’); //支持页面回跳 第二,使用session_cache_limiter方法 QUOTE: //注意要写在session_start方法之前 ...

    PHP MJPEG(php mjpg) [PHP 即時影像監控 模擬 / PHP 動態提片切換]

    header("Cache-Control: private"); header("Pragma: no-cache"); header("Content-type: multipart/x-mixed-replace; boundary=$boundary"); // Set this so PHP doesn't timeout during a long stream set_...

    request的使用.txt

    单纯的使用 xmlhttp.setRequestHeader("Cache-Control","no-cache")无效。 Cache-Control头域 Cache-Control指定请求和响应遵循的缓存机制。在请求消息或响应消息中设置Cache-Control并不会修改另一个消息处理...

    让PHP支持页面回退的两种方法[转]

    第一,使用Header方法设置消息头Cache-control header(‘Cache-control: private, must-revalidate’); //支持页面回跳  第二,使用session_cache_limiter方法 //注意要写在session_start方法之前 session_...

    让PHP支持页面回退的两种方法

    第一,使用Header方法设置消息头Cache-control header(‘Cache-control: private, must-revalidate’); //支持页面回跳  第二,使用session_cache_limiter方法 //注意要写在session_start方法之前 session_...

    crodova-httpclient:Cordova HTTP客户端

    科尔多瓦-httpclient ... Cache-Control: "private", Date: "Thu, 25 May 2011 05:05:59 GMT", X-Powered-By: "node" } } 选项 标头 用于额外的标题字段 例子 httpclient.post(URL, params, success, error,

    PHP提交表单失败后如何保留已经填写的信息

    1.使用header头设置缓存控制头Cache-control。 PHP代码如下: header('Cache-control: private, must-revalidate'); //支持页面回跳 2.使用session_cache_limiter方法。 PHP代码如下: session_cache_limiter...

    php后退一页表单内容保存实现方法

    php表单在提交之后再后退,表单的内容默认是被清空的(使用session_start的时候), 解决方法是在session_start() 之后,字符输出之前写上 复制代码 代码如下: header(“Cache-control: private”); 网页的缓存是由HTTP...

    浅析HTTP消息头网页缓存控制以及header常用指令介绍

    而如果指定了max-age值,那么在此值内的时间里就不会重新访问服务器,例如:Cache-control: max-age=5(表示当访问此网页后的5秒内再次访问不会去服务器)(2) 在地址栏回车值为private或must-reval

    c# 注销按钮返回登录

    /// /// 强制页面过期,重新向服务器请求刷新页面 /// public static void ClearClientPageCache() { HttpContext.Current.Response.Buffer = ... HttpContext.Current.Response.CacheControl = "no-cache"; }

    基于HTTP长连接的服务器推技术的php 简易聊天室

    php //chat.php header(‘cache-control: private’); header(‘Content-Type: text/html; charset=utf-8’); ?> <html> [removed] function submitChat(obj) { obj.submit(); document.getElementsByName...

    随机验证码源代码详解

    response.setHeader("Cache-Control", "on-cache"); response.setDateHeader("Expires", 0); //获取流对象向客户端写入图片 ServletOutputStream sos = response.getOutputStream();………………………………...

    断点续传java实现

    private void setHeader(URLConnection con) { con .setRequestProperty( "User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092510 Ubuntu/8.04 (hardy) Firefox/...

    异步ajax验证用户名(防止重名)

    context.Response.CacheControl = "no-cache"; context.Response.AddHeader("pragma", "no-cache"); context.Response.AddHeader("cache-control", "private"); string sql = string.Format("select * from MIS....

    如何将一个TXT文档里内容导入到文本框里

    Response.CacheControl = "no-cache" response.Charset="GB2312" '//数据返回的编码类型 显示中文数据必须 Dim objFSO Dim objText Dim ObjFile Dim strTextContent Dim objDrive '创建一个文件操作对象实例...

    Internetworking IPv6 with Cisco Routers

    Introduction, Integrated Services model, Coding of multimedia information, Reference Implementation, Traffic control, Packet scheduler, Buffers management, Packets classification, Access control, ...

    ICS delphixe10源码版

    ICS V8 is the current development release which is held in a public Version Control repository that is zipped each night for easy download. The download page above also includes the OpenSSL binaries ...

    php.ini-development

    user_ini.cache_ttl = 300 ;;;;;;;;;;;;;;;;;;;; ; Language Options ; ;;;;;;;;;;;;;;;;;;;; ; Enable the PHP scripting language engine under Apache. ; http://php.net/engine engine = On ; This directive...

Global site tag (gtag.js) - Google Analytics