简体   繁体   中英

concept of PHP Cache Control

I have read about article about PHP Cache Control.

They told about the concept and coding, but I still can't get the idea of :

Where to put those code ? put it at all page ? and at the very beginning of my php file ? I wonder browser will cache the whole HTML code ?

I saw lots for example talks about Cache is using on image file, css file. But if i want to add header on image file, does it mean I need to do a url rewrite to guide the image file request into a php file first ?

I'm assuming you mean cache regarding the browser cache. If so, what you need to know is that browsers keep tracking of every file you download. Once you visit a page for a second time, your browser first check if the file you're trying to download (ie, an image or CSS file) is already on your computer and hasn't been edited on the website in the time between your first and second visit.

If you want to achieve a browser cache, you could use some HTTP headers in order to control how the user's browser will perform the validation and cache of the given files. You could use something like this:

header("Expires: Mon, 1 Jan 2000 00: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"); 

This will force the browser to NOT cache the file. I'm showing an opposite version of what you achieve to understand why the browser behaves given a file with certain headers. The first line will tell the browser that the file you're currently visit has expired already by putting an older date. If you change the date to one in the future, you'll get the opposite functionality.

Next, the second line allows the script to tell the browser when the file the user's trying to access was edited for the last time. If the last modified date from your file on the web is older than the one on your computer (from the file you downloaded in your first visit), then your browser will download the file again, assuming it has changed since your last visit.

The third line helps explain some browsers (and also to some proxies) how they will behave when they download the file. There's different options here, you can see all of them here .

And the last one is kind of similar to the previous one, but for older systems. Here's a proper explanation .

Otherwise, there's also another option but nothing has to do with PHP: you can configure your server application to handle the cache control headers for you (if you're using PHP I'm assuming Apache or Nginx) so you don't need to rewrite everything or pass it to a PHP file either.

If you're using Apache, you could use something like this in a .htaccess file:

# 480 weeks 
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"> 
Header set Cache-Control "max-age=290304000, public" 
</FilesMatch>   

Or, if you're using Nginx, in the part of the configuration who handles your site you could add:

location ~* \.(css|js|gif|jpe?g|png)$ { 
expires 168h; 
add_header Pragma public; 
add_header Cache-Control "public, must-revalidate, proxy-revalidate"; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM