sending cache-control headers not working in Codeigniter

最后都变了- 提交于 2020-02-06 04:28:23

问题


I'm probably missing something obvious, but my Codeigniter app is not sending headers when I ask it to. So in any controller or the extended MY_Controller:

$this->output->set_header("Last-Modified: " . gmdate( "D, j M Y H:i:s" ) . " GMT");
$this->output->set_header("Expires: " . gmdate( "D, j M Y H:i:s", time() ) . " GMT");
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
$this->output->set_header("Cache-Control: post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache");
$this->output->set_header("Steve: it aint working you know");

And the headers I get are:

HTTP/1.1 200 OK
Date: Mon, 19 Mar 2012 18:03:06 GMT
Server: Apache/2.2.20 (Ubuntu)
X-Powered-By: PHP/5.3.6-13ubuntu3.6
Last-Modified: Mon, 19 Mar 2012 18:03:06 GMT
Expires: Sat, 01 Jan 2000 00:00:01 GMT
Cache-Control: post-check=0, pre-check=0, max-age=0
Pragma: no-cache
Steve: it aint working you know
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 10780
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html

So I know it's sending headers from the Steve: header but it's not sending my Cache-Control settings. If I comment out the Cache-Control setting, it still displays the same value.

Where could this be being overridden? Is it Codeigniter, PHP or Apache?


回答1:


I would check your Apache config, particularly how mod_expires and the Header Directive are configured.

Note that the "header is modified just after the content handler and output filters are run, allowing outgoing headers to be modified." So Apache may be set to modify the headers you've set in PHP.

I'm no Apache expert, but this is what I had to work with to fix a similar issue.




回答2:


Steps I took: added the following to my .htaccess file.

<IfModule mod_headers.c>
Header add Cache-Control:  "no-store, no-cache, must-revalidate"
</IfModule>

ran

    sudo a2enmod headers
    sudo service apache2 restart


来源:https://stackoverflow.com/questions/9775487/sending-cache-control-headers-not-working-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!