找回密码
 注册
查看: 783|回复: 0

header("Cache-control: private"); 在php中是什么意思

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2022-5-31 09:50:43 | 显示全部楼层 |阅读模式
网页的 缓存是由HTTP消息头中的“ Cache-control”来控制的,常见的取值有private、no-cache、max-age、must-revalidate等,默认为private。其作用根据不同的重新浏览方式分为以下几种情况:6 L4 N0 t( Y8 B1 ~7 }  c

! H0 L% u6 d! V(1) 打开新窗口
) n/ J, Z  b7 j" r/ F2 h0 J值为private、no-cache、must-revalidate,那么打开新窗口 访问时都会重新 访问服务器。0 ?* Q# B6 P& S
而如果指定了max-age值,那么在此值内的时间里就不会重新 访问服务器,例如:
7 e# |' q, x6 H$ F0 ^  }7 h7 B5 e2 |Cache-control: max-age=5(表示当 访问此网页后的 5秒内再次 访问不会去服务器)
: z# c6 L) z8 k% O% V- I& y! \! C  Y! J3 c
(2) 在地址栏回车, ^# B& u- D2 N/ O
值为private或must-revalidate则只有第一次访问时会访问服务器,以后就不再访问。
) f: |& ~; Q; J值为no-cache,那么每次都会访问。
* c0 ]4 }7 O/ f; z值为max-age,则在过期之前不会重复访问。- o, X+ S% @* H2 o' F0 z8 }

6 o( W! D+ S0 Q( Q1 `, s: A/ o; d(3) 按后退按扭
" n% c! O& P- Z8 R+ u值为private、must-revalidate、max-age,则不会重访问,. }/ M) x, }- X! G
值为no-cache,则每次都重复访问
3 j/ d. F! h# G1 C" A7 U5 J3 x# T) @% P2 n
(4) 按刷新按扭/ H+ K: G+ \5 U( E% c$ t4 F
无论为何值,都会重复访问1 Z8 L. V  ~/ ^% i% ]: Y4 H

( C0 I( s* S6 z- vCache-control值为“no-cache”时,访问此页面不会在Internet临时文章夹留下页面备份。
$ W5 A0 r) B" D) ~- s/ V; o/ u. n# m8 J" h$ o9 L/ t/ k6 {
另外,通过指定“Expires”值也会影响到 缓存。例如,指定Expires值为一个早已过去的时间,那么访问此网时若重复在地址栏按回车,那么每次都会重复访问: Expires: Tues, 31 MAY 2022 9:50:00 GMT
7 j- @. e, S3 X& l" a1 L5 u+ U' [, u2 Y. a; J; j* d. f4 ]; X
比如:禁止页面在IE中缓存) w  i0 z! m; W  T7 |. I) a
% U4 f* o& C7 w0 R2 r
http响应消息头部设置:
7 y9 i. g8 g7 M# I& ^, [, m3 E# F$ i* ^8 }
CacheControl = no-cache) _! Q" k! u  h- `+ D3 T7 y: x  n
Pragma=no-cache
1 u. f# d1 {. ]0 {* GExpires = -10 M' k0 _. b6 X* S/ o, f; J5 A
, S( [6 R/ |! Y- g0 J5 Y
Expires是个好东东,如果服务器上的网页经常变化,就把它设置为-1,表示立即过期。如果一个网页每天凌晨1点更新,可以把Expires设置为第二天的凌晨1点。
% a4 m+ f* e: _! i* g( W: Q6 e4 }, q# h% m, `) J9 W8 e
当HTTP1.1服务器指定 CacheControl = no-cache时,浏览器就不会 缓存该网页。
' N' K4 c3 }) h  n; h  `# h2 J
$ [% [$ H5 Q/ T* s5 ]旧式 HTTP 1.0 服务器不能使用 Cache-Control 标题。
' e. K/ Z3 y1 O/ I7 C$ I) i( b所以为了向后兼容 HTTP 1.0 服务器,IE使用Pragma:no-cache 标题对 HTTP 提供特殊支持。. V1 \: {3 o* w5 `/ |
如果客户端通过安全连接 ( https://)/与服务器通讯,且服务器在响应中返回 Pragma:no-cache 标题,( Q' \/ y0 I$ L4 A
则 Internet Explorer不会 缓存此响应。注意:Pragma:no-cache 仅当在安全连接中使用时才防止缓存,如果在非安全页中使用,处理方式与 Expires:-1相同,该页将被缓存,但被标记为立即过期header常用指令
( M) h& W) X4 Y7 ~/ ^. k& ]1 Cheader分为三部分:
+ }  G2 N7 w' j" w9 c8 h" r第一部分为HTTP协议的版本(HTTP-Version);
% k0 n# m6 x3 a2 n3 u第二部分为状态代码(Status);- h8 ?) f+ z( }( Z! V
第三部分为原因短语(Reason-Phrase)。, j4 S& @5 N0 b

% z4 N9 C6 Y+ }, o# G# ?$ [1 o' L! o// fix 404 pages:   用这个 header指令来解决URL重写产生的404   header
9 v5 b6 k9 \. ^header('HTTP/1.1 200 OK');   
' E) e* K  L8 T. k4 K4 w  ~
, P/ ^5 F4 U& s% i' s6 u// set 404 header:   页面没找到
% n) n5 ]1 r  N. y& a1 x% e# f; ^header('HTTP/1.1 404 Not Found');   
! I, w# t# D+ l3 N
3 S1 l" p% J( ?7 m3 a! H" h. i% E//页面被永久删除,可以告诉搜索引擎更新它们的urls
5 E. e/ a) C0 O% w$ a* C* y4 u// set Moved Permanently header (good for redrictions)   
/ }/ Y% z* f5 z# M7 \, u- O// use with location header   4 s9 Y8 o. E' g+ M
header('HTTP/1.1 301 Moved Permanently');  
" e. |' u/ l  X& N, L  D4 F6 p% ~* w: U& Q: t+ j
// 访问受限/ ]1 O* \  e8 ]0 m
header('HTTP/1.1 403 Forbidden');
* U8 V4 x- ]% O% g( C
8 Y  F  t& z8 v. Z// 服务器错误
$ E% [" V6 y, P" f5 K; p! Nheader('HTTP/1.1 500 Internal Server Error');
$ W  R( g, D/ _3 T9 }" D7 }
# e6 w; ?8 S6 k, \' u: O// 重定向到一个新的位置( q1 P- F8 [9 M: k
// redirect to a new location:   . T% r; B7 c. |( x- T! G' \
header('Location: http://www.sina.com.cn);   
) X; y% x5 M: ]6 }  z3 `1 U  V
# o3 N& {! n( C* G5 S0 h6 d: a延迟一段时间后重定向
$ r3 s) T( q' g# b6 f- n. t// redrict with delay:   
$ ?7 g3 U& Y) G" Y- y7 a) ?header('Refresh: 10; url=http://www.sina.com.cn');   
+ u2 \) p. \. R1 e* N' ]' Pprint 'You will be redirected in 10 seconds';   ) ]: U; Q; k+ g/ U# I

* E1 s  N! N# B: k  c8 X// 覆盖 X-Powered-By value6 T" L' P' N. O$ |8 H1 G# i
// override X-Powered-By: PHP:   
9 b- S  B3 \. n! D, l3 A+ m0 U* S( Dheader('X-Powered-By: PHP/4.4.0');   
& R6 r2 ^* W, H: k6 |  dheader('X-Powered-By: Brain/0.6b');   / ?  k7 d/ |5 z" d

2 M9 M% R7 s7 d, o+ i// 内容语言 (en = English)) M7 _% Y; [" e
//   content  language (en = English)   
4 F  V/ q0 y1 x% aheader('Content-language: en');   2 g! S/ Z, E+ L# ]8 p0 l- f
! L* o9 a  x9 b, t* G' W
//最后修改时间(在缓存的时候可以用到)" l1 [6 {  k. F) n. ]( U
// last modified (good for caching)   6 y) i7 j8 }/ Z6 ~* T* N" l, x5 B
$time = time() - 60; // or filemtime($fn), etc   
' q) N. e5 N7 x  eheader('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');   
, X, {% d( f  V
+ ^: T, k- r3 H// 告诉浏览器要获取的内容还没有更新
  W  w/ j, c3 H// header for telling the browser that the   content   9 |) a, u8 y0 F8 c
// did not get changed   
7 r+ ~% q9 N5 q: y: iheader('HTTP/1.1 304 Not Modified');   6 F- X! m+ \. }  E
, ]8 K$ @! g* r# A6 d
// 设置内容的长度 (缓存的时候可以用到):+ {" P% W. h1 P5 m2 }& h- o
// set   content  length (good for caching):   
: x3 |$ x7 \- F( S* n8 Cheader('Content-Length: 1234');   
! \+ a% y6 i, ^; z6 D9 f8 X# Y
, t: m7 f8 q: k  w// 用来下载文件:. r9 q% q8 X, \8 u1 ^( {0 Z, u
// Headers for an download:   1 E. l3 I! Y$ q7 F1 d/ k2 K- G
header('Content-Type: application/octet-stream');   1 h9 z7 N$ B- t. O* j
header('Content-Disposition: attachment; filename="example.zip"');   
0 `. F8 r2 g1 M3 Q  r3 b* theader('Content-Transfer-Encoding: binary');   ; f8 L( H) W* F8 \8 G
5 y* i0 o- t, @1 c$ T0 m! l
// 禁止缓存当前文档:/ R* ~1 d8 a) B4 P5 C
// load the file to send:readfile('example.zip');   
' ?( p- `" G5 y4 x- h// Disable caching of the current document:   , _; t1 Q4 P. p: `  k
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');   
. ]% z$ Y) N/ g+ h/ U6 _, \0 zheader('Expires: Mon, 26 Jul 1997 05:00:00 GMT');    1 Y* w  T, @1 e- W2 ^2 p% V
* |- Q5 q8 E6 O
// 设置内容类型:' b; s& Z/ F3 c
// Date in the pastheader('Pragma: no-cache');   
+ [& ^% |0 \8 x+ f, w& o// set   content  type:   
3 {6 k6 M* E  \header('Content-Type: text/html; charset=iso-8859-1');   
7 |# J5 {- j6 C/ hheader('Content-Type: text/html; charset=utf-8');   
! A1 a( @5 [2 o$ v" ]: ]8 w. M: k- nheader('Content-Type: text/plain');   
/ h5 u8 }7 z( _" J7 a
! `) t" d8 h5 |. u) i// plain text file   
2 e8 |  D) K! g8 ^4 n! \* zheader('Content-Type: image/jpeg');   
' l* r; @) x0 F1 S! Q, c6 T* s% S' Z- U2 |( {8 `8 h
// JPG picture   
* r1 _' V5 R2 N9 z! P2 _header('Content-Type: application/zip');   
* p3 v0 n3 H' P& b' U# P/ C% @% {% }6 z. c' J% {0 |% \" u
// ZIP file   0 ?' \$ t& w! O$ Q! \
header('Content-Type: application/pdf');   
! }. t& m& \$ x0 m2 x7 B
3 ~4 c4 h+ c4 L+ L/ O2 p// PDF file   
! y. z) j8 C9 |( _header('Content-Type: audio/mpeg');    ' B) ~5 J( s1 r; b8 k

) S& i. r' T$ ]4 p// Audio MPEG (MP3,...) file   8 s  t/ d: ~+ Y) F$ Z+ f% ^9 K
header('Content-Type: application/x-shockwave-flash');   
6 X8 \: l- A9 e1 T8 A
, l* E+ p6 {& G8 M* m// 显示登录对话框,可以用来进行HTTP认证
1 C8 y& r% w6 W// Flash animation// show sign in box   
# R( K# p& G+ Q6 t; K( Bheader('HTTP/1.1 401 Unauthorized');   
' T* t) t5 k, y! n5 J9 Fheader('WWW-Authenticate: Basic realm="Top Secret"');   ( T  u* ~( W& g4 K3 k( t& m% C* C
print 'Text that will be displayed if the user hits cancel or ';   
+ S" y  a( ^9 Y# I- l8 yprint 'enters wrong login data';?>7 `/ n1 b9 a' q8 O/ h. @
您需要登录后才可以回帖 登录 | 注册

本版积分规则

返回首页|Archiver|手机版|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )

GMT+8, 2026-6-12 00:12 , Processed in 0.023357 second(s), 22 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表