首頁  >  文章  >  後端開發  >  php如何清除瀏覽器快取?

php如何清除瀏覽器快取?

青灯夜游
青灯夜游原創
2020-08-21 10:55:164062瀏覽

PHP使用「header("Last-Modified:".gmdate( "D,d M Y H:i:s") . "GMT");header("Cache-Control:no-cache,must- revalidate");”來清除快取。

php如何清除瀏覽器快取?

推薦:《PHP影片教學

php清除瀏覽器快取

今天在開發過程中遇到一個小問題,後台修改一本書的封面,上傳圖片後,由於瀏覽器的快取導致修改之後,後台還是顯示原先的封面,後來加入了兩行程式碼搞定了這個問題。

header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );  
header("Cache-Control: no-cache, must-revalidate" );

說明:

header() 函數傳送原始的 HTTP 標頭到客戶端。

認識到一點很重要,即必須在任何實際的輸出被發送之前調用header() 函數

語法

header(string,replace,http_response_code)

php如何清除瀏覽器快取?

PHP header 的7種用法

1.  跳頁
header('Location:'.$url);  //Location和":"之间无空格。
2.  宣告content-type
header('content-type:text/html;charset=utf-8');
3.  回傳response狀態碼
header('HTTP/1.1 404 Not Found');
4. 在某個時間後執行跳轉
header('Refresh: 10; url=http://www.baidu.com/');  //10s后跳转。
5. 控制瀏覽器快取
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
6. .執行 http驗證
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Top Secret"');
7.執行下載操作
header('Content-Type: application/octet-stream'); //设置内容类型
header('Content-Disposition: attachment; filename="example.zip"'); //设置MIME用户作为附件
header('Content-Transfer-Encoding: binary'); //设置传输方式
header('Content-Length: '.filesize('example.zip')); //设置内容长度

更多程式相關知識,可存取:程式設計入門! !

以上是php如何清除瀏覽器快取?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn