Pragma:no-cache哪里來的?


問:為什么我在點擊瀏覽器的后退按鈕后,所有字段的信息都被清空了?

答:這是由於你在你的表單提交頁面中使用了 session_start 函數。該函數會強制當前頁面不被緩存。解決辦法為,在你的 Session_start 函數后加入 header("Cache-control: private"); 注意在本行之前你的PHP程序不能有任何輸出。

今天給同事分享了一下前端性能優化,在介紹了php文件緩存的方法后,發現一個AJAX請求的文件,在請求頭中始終有一個:Pragma:no-cache,導致這個文件不能被瀏覽器緩存。接着發現這個站幾乎所有動態頁面都有這個參數Pragma:no-cache。

開始懷疑是不是設置了Cache-control:no-cache導致的,但查遍程序沒發現。懷疑服務器的配置,沒什么異常的。
最后,同事定位到了頁面開始的session_start()身上。。。

問題果然出現在它session身上!

引用
session.cache_limiter string
session.cache_limiter 指定會話頁面所使用的緩沖控制方法(none/nocache/private/private_no_expire/public)。默認為 nocache。參見 session_cache_limiter()。
引用
string session_cache_limiter ( [string cache_limiter] )

session_cache_limiter() returns the name of the current cache limiter. If cache_limiter is specified, the name of the current cache limiter is changed to the new value.

The cache limiter defines which cache control HTTP headers are sent to the client. These headers determine the rules by which the page content may be cached by the client and intermediate proxies. Setting the cache limiter to nocache disallows any client/proxy caching. A value of public permits caching by proxies and the client, whereas private disallows caching by proxies and permits the client to cache the contents.

In private mode, the Expire header sent to the client may cause confusion for some browsers, including Mozilla. You can avoid this problem by using private_no_expire mode. The expire header is never sent to the client in this mode.

The cache limiter is reset to the default value stored in session.cache_limiter at request startup time. Thus, you need to call session_cache_limiter() for every request (and before session_start() is called).



當啟用了session后,session.cache_limiter就被默認設置為nocache,所以就在http頭里發送no-cache信息,導致頁面不會被緩存。
當然一般需要緩存的文件是不需要session的,因此去掉頭部的session_start()即可。
如果實在需要在啟用session后還需要把這個頁面緩存,則需要在session_start之前設置下session_cache_limiter('public')。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM