Cache-Control Header Modified By PHP Session?

后端 未结 2 442
慢半拍i
慢半拍i 2020-12-25 08:29

I\'m outputting an image to the browser using a Zend_Controller_Response object. It is my intention to apply caching to the image, however something is causing

相关标签:
2条回答
  • 2020-12-25 08:49

    You're right by assuming that this behaviour is connected to the session mechanism in PHP. There is a configuration setting session.cache_limiter that controls the caching HTTP headers that will be sent with the response. The default setting here is nocache which sends

    Expires: Thu, 19 Nov 1981 08:52:00 GMT 
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
    Pragma: no-cache
    

    You overwrite all of these headers within your controller besides the Cache-Control-header (you only append your max-age=3600 setting here).

    Possible solutions are:

    1. changing the PHP configuration (session.cache_limiter) to e.g. none - but this could introduce problems to other PHP applications
    2. set the session.cache_limiter on each request using session_cache_limiter()
    3. overwrite the full Cache-Control-header in your controller with the designated string

    The possible values for session.cache_limiter and session_cache_limiter() are:

    none: no header will be sent

    nocache:

    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    Pragma: no-cache
    

    private:

    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Cache-Control: private, max-age=10800, pre-check=10800
    

    private_no_expire:

    Cache-Control: private, max-age=10800, pre-check=10800
    

    public:

    Expires: pageload + 3 hours
    Cache-Control: public, max-age=10800
    
    0 讨论(0)
  • 2020-12-25 08:54

    From the Zend_Controller documentation, section 10.9. The Response Object

    setHeader($name, $value, $replace = false) is used to set an individual header. By default, it does not replace existing headers of the same name in the object; however, setting $replace to true will force it to do so.

    The problem you are having is your max-age=3600 is being appended to the cache-control header, as opposed to replacing it. Try setting the $replace parameter to true.

    0 讨论(0)
提交回复
热议问题