Right now a request is always made, which is then answered with 304 Not modified, but it would be better if the browser did not make a request at all. How do I achieve this, which headers to send, etc.? I can't seem to figure it out.
I couldn't find a solution to my question so here I am. The current code I have which I put images through to add them to cache is:
$location = base64_decode($_GET['location']);
$path = path($location);
header('X-Sendfile: ' . $path);
header('Content-type: ' . mime_content_type($path));
header('Content-Disposition: inline; filename="' . basename($path) . '"');
header('Cache-Control: private, max-age=' . 60 * 60 * 24);
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 60 * 60 * 24) . ' GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s \G\M\T', filemtime($path)));
header('Etag: "' . md5_file($path) . '"');
if ((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= filemtime($path)) ||
(isset($_SERVER['HTTP_IF_NONE_MATCH']) && trim($_SERVER['HTTP_IF_NONE_MATCH']) === $etag)
) {
header('HTTP/1.1 304');
exit();
}
readfile($path);
What I've tried: I've looked everywhere on Google, stackoverflow, other web forums, youtube, experimenting with it myself.
What I'm expecting: Right now a request is always made, which is then answered with 304 Not modified, but it would be better if the browser did not make a request at all.
Result right now: It gives me a 304 Not Modified status code.
Any help is appreciated!
This is my first question on here, so if you need more information I will try to give as much as I can.
function set_cache($period=3600){
if($period==0){
header("Cache-Control: no-cache, no-store, must-revalidate",1);
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT",1);
}else{
header_remove('Expires');
header_remove('Pragma');
header("Cache-Control: public, max-age=$period",1);
header('Expires: '.gmdate('D, d M Y H:i:s', time()+$period).' GMT', 1);
}
}
My clients never send request with such code until expiration period. The only way - clear browser cache or check "No cache" in browser's developer tools