Re: images doesn't seem to cache

From: Date: Sat, 06 Nov 2004 19:54:51 +0000
Subject: Re: images doesn't seem to cache
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-201373@lists.php.net to get a copy of this message
Your eyes are fine. You need to check for If-Modified-Since header, if the time is older than file modification time (filemtime()) send Last-Modified header and the image, else send 304 Not Modified response.
This code seems to work. Have I got it right? // Get the time the cache file was last modified $lastModified = filemtime($pPath); // Issue an HTTP last modified header header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT'); if (isset($_GET['If-Modified-Since'])) { // Split the If-Modified-Since (Netscape < v6 gets this wrong) $modifiedSince = explode(';', $_GET['If-Modified-Since']); // Turn the client request If-Modified-Since into a timestamp $modifiedSince = strtotime($modifiedSince[0]); } else { $modifiedSince = 0; } // Compare time the content was last modified with client cache if ($lastModified <= $modifiedSince) { header('HTTP/1.1 304 Not Modified'); } else { $extention = substr($path, -3); if ($extention == "jpg") header("Content-type: image/jpeg"); if ($extention == "gif") header("Content-type: image/gif"); if ($extention == "bmp") header("Content-type: image/bmp"); if ($extention == "png") header("Content-type: image/png"); readfile("$pPath"); }

« previous php.general (#201373) next »