RE: [PHP] images doesn't seem to cache
To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm On 06 November 2004 20:09, anders thoresson wrote: This code seems to work. Have I got it right? No. I have not. Sometimes the images are viewed from the cache, just to get downloaded from the server again next time, just a minute later, when I try again. My local development server is running IIS, my production server is running Apache. Where is the best place to look for If-Modified-Since? Is $_GET['If-Modified-Since'] a safe bet? For Apache, I found it in $_SERVER['HTTP_IF_MODIFIED_SINCE']; if IIS passes it to PHP, I would definitely expect it to be in $_SERVER[]. Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Headingley Campus, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] images doesn't seem to cache
anders thoresson wrote: This code seems to work. Have I got it right? No. I have not. Sometimes the images are viewed from the cache, just to get downloaded from the server again next time, just a minute later, when I try again. My local development server is running IIS, my production server is running Apache. Where is the best place to look for If-Modified-Since? Is $_GET['If-Modified-Since'] a safe bet? No, it's a http header, not a get parameter. I'm not sure if this header is made available under IIS. Under apache you could use apache_request_headers(). -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] images doesn't seem to cache
it won't be a php-parameter. Seen as the script isn't executed when the server decides it is the same as the cached version. So only if it deems not to be, then it runs the script, and when it does that, the script doesn't need to know anything about modified-since, because that checks has long since been passed. Really? It's not until the script is executed that the acutal image is accessed. Until, it's only refered to as a picture id, and int-value. Your suggestion was to use If-Modified-Since. How and where should I do a check for it? Best regards, Anders -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] images doesn't seem to cache
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
Re: [PHP] images doesn't seem to cache
This code seems to work. Have I got it right? No. I have not. Sometimes the images are viewed from the cache, just to get downloaded from the server again next time, just a minute later, when I try again. My local development server is running IIS, my production server is running Apache. Where is the best place to look for If-Modified-Since? Is $_GET['If-Modified-Since'] a safe bet? //Anders -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] images doesn't seem to cache
Anders Thoresson wrote: This code seems to work. Have I got it right? No. I have not. Sometimes the images are viewed from the cache, just to get downloaded from the server again next time, just a minute later, when I try again. My local development server is running IIS, my production server is running Apache. Where is the best place to look for If-Modified-Since? Is $_GET['If-Modified-Since'] a safe bet? it won't be a php-parameter. Seen as the script isn't executed when the server decides it is the same as the cached version. So only if it deems not to be, then it runs the script, and when it does that, the script doesn't need to know anything about modified-since, because that checks has long since been passed. This is all an issue between browser and server, and php can't interfere in between those. However, to do something about it, change the headers that you send abou caching. These are the pragma headers for HTTP/1.0 (?), and the Cache-Control headers in HTTP/1.1 //Anders -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] images doesn't seem to cache
Hi, I put all my images outside the web root, the prevent direct access, and then access them with a img-tag like this: img src=fnc_get_image.php?path=?=$path;? / where fnc_get_image.php is: // Check if user is logged in require_once 'global_includes.php'; $user = new User(); // Get path to image for display $path = $_GET['path']; // Prepend path - prevents misuse $pPath = /home/username/albums/ . $path; header(Cache-Control: private); $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); There is one slight problem though: To my eyes, it looks like the images are downloaded from the server every time. Is this a side effect of this method or is it just a optical illusion? Is there a way to tell for sure if the image is downloaded or displayed from the browser cache? I've tried with and without a Cache-Control header. It doesn't seem to do any difference at all. Best regards, Anders -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] images doesn't seem to cache
anders thoresson wrote: Hi, I put all my images outside the web root, the prevent direct access, and then access them with a img-tag like this: img src=fnc_get_image.php?path=?=$path;? / where fnc_get_image.php is: // Check if user is logged in require_once 'global_includes.php'; $user = new User(); // Get path to image for display $path = $_GET['path']; // Prepend path - prevents misuse $pPath = /home/username/albums/ . $path; header(Cache-Control: private); $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); There is one slight problem though: To my eyes, it looks like the images are downloaded from the server every time. Is this a side effect of this method or is it just a optical illusion? Is there a way to tell for sure if the image is downloaded or displayed from the browser cache? I've tried with and without a Cache-Control header. It doesn't seem to do any difference at all. 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. HTH -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php