找回密码
 注册
查看: 782|回复: 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。其作用根据不同的重新浏览方式分为以下几种情况:2 X9 D* j- j# G) e; H+ s$ b

2 w" o' J$ {, O1 i9 s" W(1) 打开新窗口
4 r, X) {6 p) S4 k( O& S1 k值为private、no-cache、must-revalidate,那么打开新窗口 访问时都会重新 访问服务器。4 Q3 Q1 A$ ]7 T8 S& D* ~
而如果指定了max-age值,那么在此值内的时间里就不会重新 访问服务器,例如:
5 F# P8 C4 i: |( w1 {Cache-control: max-age=5(表示当 访问此网页后的 5秒内再次 访问不会去服务器)
6 |1 A/ ]* g) a* @5 A
5 ~$ w2 m% f3 ]& _& }# P(2) 在地址栏回车
5 o. ]& U, T% c! h值为private或must-revalidate则只有第一次访问时会访问服务器,以后就不再访问。
" V- y- {; J6 e& f+ J: I! O值为no-cache,那么每次都会访问。! v0 |. p( S2 P. a1 r
值为max-age,则在过期之前不会重复访问。
# x3 D3 J. b# X5 y' P
8 S) U9 O; p8 T: s" P# \(3) 按后退按扭
4 j  S. u" t& ?9 s2 [值为private、must-revalidate、max-age,则不会重访问,
/ m% F* {- ?- {) \6 K% _( A值为no-cache,则每次都重复访问
7 k% l/ Y+ l1 y! |3 C' |1 ^* S7 |4 W
(4) 按刷新按扭, l$ P& s2 V0 ^1 Y9 v/ J* g: z1 r
无论为何值,都会重复访问
3 C; P$ X/ y, h0 d5 c
# h' O: O: v/ `9 ^4 RCache-control值为“no-cache”时,访问此页面不会在Internet临时文章夹留下页面备份。2 |+ _; P$ @+ ]) ^1 \4 t

) p' s) N- B& z/ K! V8 c+ i2 r5 x$ U另外,通过指定“Expires”值也会影响到 缓存。例如,指定Expires值为一个早已过去的时间,那么访问此网时若重复在地址栏按回车,那么每次都会重复访问: Expires: Tues, 31 MAY 2022 9:50:00 GMT: p7 X& @" e) J7 @8 n" P. n7 [
6 A( D: B3 Q* V/ c
比如:禁止页面在IE中缓存
% u9 h0 E  n8 d- w  }, W
' l4 Y0 {, y  c0 B% ehttp响应消息头部设置:' B3 Q; s0 M. ^

9 [0 D5 ?0 H( ]. c: X6 aCacheControl = no-cache
8 m$ |9 h  h4 U( {6 JPragma=no-cache: z; \  e" X( u' Q2 J2 c
Expires = -18 c1 g! H9 J) h; W8 Z+ N& Y
. i0 N& W3 }; Z0 N+ }7 G
Expires是个好东东,如果服务器上的网页经常变化,就把它设置为-1,表示立即过期。如果一个网页每天凌晨1点更新,可以把Expires设置为第二天的凌晨1点。$ r3 v7 C* o) d9 v( K
# N; D+ E. I( Q1 b% z
当HTTP1.1服务器指定 CacheControl = no-cache时,浏览器就不会 缓存该网页。
  H% R1 g6 L" a  y/ y* Q' \
" t' d+ D/ M: U/ x% G$ D旧式 HTTP 1.0 服务器不能使用 Cache-Control 标题。4 D# t7 |; a) f3 M, F; L( ^4 S
所以为了向后兼容 HTTP 1.0 服务器,IE使用Pragma:no-cache 标题对 HTTP 提供特殊支持。
+ x2 O; M/ A; t. \如果客户端通过安全连接 ( https://)/与服务器通讯,且服务器在响应中返回 Pragma:no-cache 标题,9 T) s0 p. ?( T9 b0 j9 G, q) k
则 Internet Explorer不会 缓存此响应。注意:Pragma:no-cache 仅当在安全连接中使用时才防止缓存,如果在非安全页中使用,处理方式与 Expires:-1相同,该页将被缓存,但被标记为立即过期header常用指令+ B% a! R& p# n& m% `6 g/ o* Q# W
header分为三部分:
+ n( X+ v1 n; T- u- {第一部分为HTTP协议的版本(HTTP-Version);
5 [: Z6 z( E5 e$ t6 O6 d( F% X第二部分为状态代码(Status);
1 t& P" X; N% D* X  [, A" U8 t第三部分为原因短语(Reason-Phrase)。
" v  h, a9 Z" y9 z* c* f5 }" w" u- {" W: M& Z$ T& L! _4 B
// fix 404 pages:   用这个 header指令来解决URL重写产生的404   header
. _; F+ z4 x- ]$ r6 w- C% Sheader('HTTP/1.1 200 OK');   ; @. g! c  E1 H( o- P' p

! a, p$ s& u8 ^9 x& q// set 404 header:   页面没找到$ v; O( P. [2 `+ E
header('HTTP/1.1 404 Not Found');   8 t' V' H* A" U6 W" S

, I8 y5 R! `+ K( q$ i' |//页面被永久删除,可以告诉搜索引擎更新它们的urls5 [; Q* G8 {7 Q6 d, ?/ P# x: P9 W% Z
// set Moved Permanently header (good for redrictions)   ( s. [6 R3 [! r6 H) e; ^
// use with location header   
0 O. Z+ [2 Q: ]% F6 R" Gheader('HTTP/1.1 301 Moved Permanently');  ; L0 Q( ~  Z) E0 [! |

# k9 p: J* {' x/ Q// 访问受限
1 o- e, L8 m# J0 m  o! Dheader('HTTP/1.1 403 Forbidden');
5 Z- p$ y, ^7 f" F
7 y3 ~1 A3 P. V. Y0 ~; {9 E# T- P2 @// 服务器错误
0 H$ [" h, [" v0 R8 jheader('HTTP/1.1 500 Internal Server Error');
# n5 V+ ^% ?8 {" K6 ]
. d: Z" @7 M9 y: M/ r5 U// 重定向到一个新的位置  s8 s% P2 z* s4 M' x, G! n" X
// redirect to a new location:   
% m+ ]/ H! ?" N, o1 Yheader('Location: http://www.sina.com.cn);     B, C+ B4 O# U- h6 [3 Y+ D7 s

8 Q/ z$ }2 K, j2 g$ q2 M# X延迟一段时间后重定向! v% t: T8 r- w" |- b' o
// redrict with delay:   
, R6 G8 _& w/ A5 ?0 oheader('Refresh: 10; url=http://www.sina.com.cn');   
( O# G! p0 q$ kprint 'You will be redirected in 10 seconds';   
- f5 X; O7 C: o$ m1 Y+ C6 d4 x2 z, s! i' I/ f
// 覆盖 X-Powered-By value' u7 o% a6 o5 l0 u1 z: Z1 P0 h( M
// override X-Powered-By: PHP:   0 t6 I6 V* m, w' a% @, z3 K
header('X-Powered-By: PHP/4.4.0');   * Q9 T+ f$ D: _! U& r
header('X-Powered-By: Brain/0.6b');   & {, S3 _- {; k0 b9 C
* \( U' @9 B1 [2 I6 r2 x' i
// 内容语言 (en = English)& y* j7 J- f$ h2 k- i
//   content  language (en = English)   ) G$ Q; h/ n) j+ I: R1 a/ ]
header('Content-language: en');   # @! X4 T, {" J  ~* h3 N& d* d
% N- w! \& S/ A- {) u& s6 v
//最后修改时间(在缓存的时候可以用到)8 k3 H% g6 O. [
// last modified (good for caching)   3 X: W& R; ?2 A0 Z" w/ U, O
$time = time() - 60; // or filemtime($fn), etc   
) q( d. A# B+ J+ Jheader('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');   ( g6 l; p, o8 Z$ {0 S' M, J) F

( N' `8 C! b4 |  Z3 R4 Q2 S* y// 告诉浏览器要获取的内容还没有更新
7 z/ G7 ]% d: C4 {  N- k& U// header for telling the browser that the   content   8 I) v. H3 V6 Q; U/ Q) l% Y+ F  ~
// did not get changed   , a: F) I7 @5 K% K
header('HTTP/1.1 304 Not Modified');   ; w3 n1 a, ?6 X7 ~1 P# {
: n1 q( ?$ ~( A: [( g  @# x
// 设置内容的长度 (缓存的时候可以用到):; q" U/ c+ K/ |9 T% c, O7 q' Q: d
// set   content  length (good for caching):   * p; o; y& M+ z5 d* w  x4 T6 e2 w
header('Content-Length: 1234');   8 \& Q' T( f% ]) k. |
5 w9 {( c, c7 }  c
// 用来下载文件:# w! X9 {4 c& ?6 b3 j- N+ \1 k
// Headers for an download:   
/ G& x& T* z6 Z; X  v  eheader('Content-Type: application/octet-stream');   8 x: @# B9 F0 F! _' V
header('Content-Disposition: attachment; filename="example.zip"');   & q9 O( S: x3 h) b: k
header('Content-Transfer-Encoding: binary');   
5 x& w* h; y* l: ~: G* a, B* E4 P: ~: K9 N# y( S9 p' _5 |
// 禁止缓存当前文档:
1 `5 V: h; e# Q5 l// load the file to send:readfile('example.zip');   
+ i" ?- @* \4 j/ M2 t// Disable caching of the current document:   & m, Q' q3 m3 Y$ ^/ o' O
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');   1 y1 _+ G0 G) N, `
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');   
7 e3 x9 h, B2 |5 L: ]" d) y# l1 b3 p  H4 x; p3 c
// 设置内容类型:
: G5 n' u. a9 V1 V9 z; B* Z// Date in the pastheader('Pragma: no-cache');   ! m' h5 W6 C. C2 t
// set   content  type:   * H# n) M7 D! c8 S
header('Content-Type: text/html; charset=iso-8859-1');   ' q' S3 c5 y+ {0 i( I
header('Content-Type: text/html; charset=utf-8');   
0 U6 q- z: t7 C  yheader('Content-Type: text/plain');   - z: B3 J, k% L8 H( [! E  D
8 ^  j# @7 @- `! U9 B/ X3 f2 f
// plain text file   
* m# Y9 B1 X: a. v) F! \1 R1 cheader('Content-Type: image/jpeg');      x6 o! l! u$ P* C1 z

% \/ E, E" E8 ^) I0 w" {7 {4 g. w// JPG picture   
' A. B! S$ I9 {1 k: Oheader('Content-Type: application/zip');    : a( J& ?, V8 a3 }, ]8 u) P7 ~

9 \1 e5 y) h2 V2 F- ]& e- @// ZIP file   
! u0 [5 l/ `+ {1 E& x/ l4 u% a9 mheader('Content-Type: application/pdf');    3 {2 N( T, C+ e; O4 S$ `" Z; i
% v. N# h6 C( y6 A  Q& N
// PDF file   4 v7 {  }& B9 d8 Z
header('Content-Type: audio/mpeg');    ) g- m9 c7 B/ n5 M

8 U/ m/ G) u" X9 u, m3 B// Audio MPEG (MP3,...) file   
; ~4 Q' ~- b8 m% I) @header('Content-Type: application/x-shockwave-flash');   
4 _; s  g* Y) G6 g8 p* H' x. s0 g7 a) W# T
// 显示登录对话框,可以用来进行HTTP认证7 |" M8 }. L1 w7 a1 j7 o: _* n
// Flash animation// show sign in box   : S6 ~( l$ }4 ~/ j# H' p
header('HTTP/1.1 401 Unauthorized');   
; w, g5 @4 k8 N# B8 Dheader('WWW-Authenticate: Basic realm="Top Secret"');   & [3 _- F% T; a1 ^- A: V. w
print 'Text that will be displayed if the user hits cancel or ';   0 f- a$ K* Q2 G: s; ?
print 'enters wrong login data';?>
' a2 g1 V# S7 ^/ ?$ L  r
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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