FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Caching Dynamic Images -- no luck





Aredon
SITUATION
I'm using a "blob" field in MySQL to store avatars for registered users.
Then "avatar.php?id=1" will return to the client the image associated with the specific user -- which executes a MySQL SELECT query and echos the data to the client. My system is up and running however I want to enhance upon the system.

PROBLEM
I want the user's avatars avatar.php?id=1 to be cached so the user's browser won't even attempt to send an HTTP Request to PHP which would only receive the same image every time. I've thought about using a folder directory to store the images yet I'd rather be using a database to store the avatars. I'm not worried about user's changing their avatars and the cached image being out-of-date. My solution for this is adding a timestamp to the query string as a parameter representing the last time the image was updated. "avatar.php?id=1&lastupdated=1167636819"
then if the user updated their avatar image in the future then the link to their avatar would change to: "avatar.php?id=1&lastupdated=1168403816"
which would force user's browsers to re-cache the image due to the change in the query string.

ASSISTANCE
You dig what I'm saying? So I'm just trying to do something as simple as caching a PHP file as an image on the client to spare the browser the extra lag of sending an HTTP GET Request while the cache image could instead be used. I'm pretty sure this would be done with the header() PHP function yet I've had no luck getting either IE or Firefox to cache the image... If this can be done, I'd want to add the max length to the cache as possible because the cached file will never need to be updated because if the image in the database were ever changed then the querystring would be changed and browser would cached it separately.

FINAL QUESTION
So... How would I go about this simple task of caching the PHP file?
hexkid
This works for me (with Firefox, checked with Live HTTP Headers extension)
Code:
<?php
$img = "" .
"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52" .
"\x00\x00\x00\x64\x00\x00\x00\x64\x01\x03\x00\x00\x01\x3d\x2b\x37" .
"\x81\x00\x00\x00\x2b\x74\x45\x58\x74\x43\x72\x65\x61\x74\x69\x6f" .
"\x6e\x20\x54\x69\x6d\x65\x00\x54\x68\x75\x20\x38\x20\x44\x65\x63" .
"\x20\x32\x30\x30\x35\x20\x32\x32\x3a\x33\x39\x3a\x35\x31\x20\x2d" .
"\x30\x30\x30\x30\xf0\x21\x0d\xd5\x00\x00\x00\x07\x74\x49\x4d\x45" .
"\x07\xd5\x0c\x08\x16\x29\x17\x51\x5d\x27\x3c\x00\x00\x00\x09\x70" .
"\x48\x59\x73\x00\x00\x0b\x12\x00\x00\x0b\x12\x01\xd2\xdd\x7e\xfc" .
"\x00\x00\x00\x04\x67\x41\x4d\x41\x00\x00\xb1\x8f\x0b\xfc\x61\x05" .
"\x00\x00\x00\x06\x50\x4c\x54\x45\x00\x00\x00\xff\xff\xff\xa5\xd9" .
"\x9f\xdd\x00\x00\x01\x53\x49\x44\x41\x54\x78\xda\x8d\x91\x31\x72" .
"\xc4\x20\x0c\x45\xc5\x50\x28\x1d\x29\xd3\xe9\x1a\xe9\xc8\x91\x52" .
"\xa6\xd3\x1e\xcd\x47\x72\x49\x47\x90\x91\xb1\x3e\x45\x26\x9e\x59" .
"\xbd\x7d\x83\x25\x46\xdf\x44\xe3\x79\x23\xfa\x20\xfa\x24\xfa\x21" .
"\x6a\x94\x1a\xe5\x46\xdc\xa8\x34\x92\x46\xb5\x91\xb6\x7f\xbd\xd3" .
"\x9b\x0d\x7b\x8d\x5f\xb6\x52\xac\x54\x2b\x7d\x94\x6c\xa5\x58\xa9" .
"\x56\xfa\x28\xd9\x4a\xb1\x52\xad\xdc\x8f\xfd\x4b\x6b\x0a\xaf\x51" .
"\xb2\xe6\xe9\x3d\x34\xad\xc9\xbc\xc6\xcb\xba\x43\xef\x8b\xd2\xba" .
"\x8d\xd7\x95\xb2\xee\xd5\xe7\xf2\xaf\x89\x74\x4c\xb2\x53\x9c\x3a" .
"\x99\xfa\x24\x3b\xc5\xa9\x93\xe3\xb6\x63\x1e\x4f\x8a\x53\x27\xc7" .
"\xf1\x45\x76\x8a\x53\x27\xed\xd8\xc8\x4e\x71\xea\xe4\x75\x3c\xc8" .
"\x4e\x71\xaa\x13\x1f\xdf\x87\x0e\xdc\x2b\x6f\xfb\x95\x6d\xcf\x8a" .
"\xfb\x52\xc7\xbd\xf3\xb6\x7f\xd9\x72\xa8\x98\x07\x75\xcc\x25\x6f" .
"\xf9\x94\x2d\xa7\x8a\x79\x51\xc7\xdc\xf2\x96\x5f\xd9\x72\xac\x98" .
"\x27\x75\xcc\x35\x6f\xf9\x96\x2d\xe7\x8a\x79\x53\xc7\xdc\xf3\x96" .
"\x7f\xd9\xbe\x43\xfd\xe3\x7b\xbc\x83\x7d\x47\x49\x67\x34\x06\x13" .
"\x30\x8d\x96\x7a\x34\x06\x13\x30\x8d\x36\x96\x39\x63\x5b\x34\x01" .
"\xd3\x68\x96\xc1\x19\xdb\x82\x09\x98\x46\xbb\xa2\x3b\x63\xdb\x63" .
"\x02\xa6\xd1\x66\xe2\x67\x6c\x5b\x26\x60\x1a\x6d\xb6\xdd\xc6\x60" .
"\x02\xa6\xd1\xbc\xcd\x8d\xc1\x04\x4c\xa3\xdd\x6d\xd3\x18\x4c\xc0" .
"\x34\xda\x6a\xbb\x8c\xc1\x04\x4c\xa3\x3d\x6d\x66\x0c\x26\x60\x1a" .
"\x2d\xb4\x0d\x63\x30\x01\xd3\x68\xbf\x6d\x27\x7e\x92\x53\x37\x31" .
"\xea\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82\x0a" .
"";

$headers = apache_request_headers();
// Checking if the client is validating his cache and if it is current.
if (isset($headers['If-Modified-Since']) && (strtotime($headers['If-Modified-Since']) == gmmktime(0, 0, 0, 1, 8, 2007))) {
  // Client's cache IS current, so we just respond '304 Not Modified'.
  header('Last-Modified: ' . gmdate('D, d M Y H:i:s', gmmktime(0, 0, 0, 1, 8, 2007)) . ' GMT', true, 304);
} else {
  // Image not cached or cache outdated, we respond '200 OK' and output the image.
  header('Last-Modified: ' . gmdate('D, d M Y H:i:s', gmmktime(0, 0, 0, 1, 8, 2007)) . ' GMT', true, 200);
  header('Content-Type: image/png');
  echo $img;
}

?>


Feel free to tweak it Smile
Aredon
Worked like a charm.
Code:

...
$row=mysql_fetch_object($result);

$headers = apache_request_headers();
if (isset($headers['If-Modified-Since']) && (strtotime($headers['If-Modified-Since']) == $row->time_stamp)) {
  // Client's cache IS current, so we just respond '304 Not Modified'.
  header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $row->time_stamp) . ' GMT', true, 304);
exit;
} else {
  // Image not cached or cache out-of-date, so we respond '200 OK' and output the image.
  header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $row->time_stamp) . ' GMT', true, 200);
 header("Content-Type: {$row->type}");
 echo $row->bindata;
exit;
}

?>

Kudos to you hexkid!
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.