Re: images doesn't seem to cache

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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");
}

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux