 |
 |
Browser Cache & PHP?
|
 |
|
 |
|
Registered User
Join Date: Apr 2003
Status:
Offline
|
|
When a PHP script like the one below is run, is the image placed in the browser cache and would subsequent runs of the script use the image in the cache, or would another copy of the image be downloaded from the server.
If it does download another copy, how would one make it use the cached copy instead?
Code:
<?
Header("Content-Type: image/jpeg");
$imagefile=fopen("./01.jpg","r");
fpassthru($imagefile);
?>
G.
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 2001
Location: Are Eye
Status:
Offline
|
|
Nothing happens within the browser as far as I know because that script doesn't ever send anything to the browser.
javascript may be a better solution for you to cache images within a browser, or perhaps just loading them with the css attribute of visibility: hidden.
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status:
Offline
|
|
Originally posted by Gadifae:
[B]When a PHP script like the one below is run, is the image placed in the browser cache and would subsequent runs of the script use the image in the cache, or would another copy of the image be downloaded from the server.
If it does download another copy, how would one make it use the cached copy instead?
You can certainly prevent an image from caching, but I've not seen any ways of encouraging the use of cached files - at least not server-side.
If you're more keen for a user to see cached images, you may think about a getting an http proxy (like Apache's mod_proxy, or Squid) in front of your webserver and set it up so that images always cache for longer.
Any particular reason why you need this?
|
|
Computer thez nohhh...
|
| |
|
|
|
 |
|
 |
|
Registered User
Join Date: Apr 2003
Status:
Offline
|
|
Originally posted by Simon Mundy:
You can certainly prevent an image from caching, but I've not seen any ways of encouraging the use of cached files - at least not server-side.
If you're more keen for a user to see cached images, you may think about a getting an http proxy (like Apache's mod_proxy, or Squid) in front of your webserver and set it up so that images always cache for longer.
Any particular reason why you need this?
I've been toying around with an image gallery (see the thread titled "Controlling Image Display Location") and thought I would play around with a PHP solution for a little bit to see how one might stack up to the JavaScript one I already have in place.
My curiosity over whether a PHP script would take advantage of the browser cache is so a visitor, should they want to see a photo again in a few minutes later wouldn't have to have another copy pulled down from the server. Testing it this morning, it seemed like it might be downloading a new copy each time since there seemed to be a speed hit that didn't exist with the JavaScript version.
G.
|
|
|
| |
|
|
|
 |
|
 |
|
Registered User
Join Date: Apr 2003
Status:
Offline
|
|
Originally posted by registered_user:
Nothing happens within the browser as far as I know because that script doesn't ever send anything to the browser.
When you say that script doesn't send anything to the browser, do you mean that running the script will not cause an image to appear? If you mean something different please explain. The script does send something to my browser, or at least running it causes an image to display in my browser window.
javascript may be a better solution for you to cache images within a browser, or perhaps just loading them with the css attribute of visibility: hidden.
Right now, the solution I am using is a JavaScript one, I'm merely tinkering with PHP since this seems like the best way to learn anything about such things.
Thanks for the replies Simon & Registered User.
G.
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status:
Offline
|
|
Originally posted by Gadifae:
My curiosity over whether a PHP script would take advantage of the browser cache is so a visitor, should they want to see a photo again in a few minutes later wouldn't have to have another copy pulled down from the server. Testing it this morning, it seemed like it might be downloading a new copy each time since there seemed to be a speed hit that didn't exist with the JavaScript version.
G.
That's right - Javascript is client-side, and is heavily tied in with the browser's current state. That's why rollovers work the way they do - you load them into memory with Javascript, then each time the image is called it pulls it straight out of memory rather than fetching a new copy (unless it doesn't exist in memory, obviously!).
If the user is flicking through a Javascript-enabled gallery, they're likely not to have to reload a pre-cached image unless their cache is particularly full. If, however, you've got loads of huge JPEGs, the visitor's browser may decide to reload them for every request.
For your purposes, it sounds like Javascript is the way to go (with the caveat, of course, that non-javascript users will be SOL!)
|
|
Computer thez nohhh...
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status:
Offline
|
|
You would need to play with the http headers (rather easy), in fact PHP's documentation on the subject even includes an example that should get you on the right track.
Note: just because you tell a browser that it should cache, does not mean that it will.
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 2001
Location: Are Eye
Status:
Offline
|
|
Sorry about that, my n00b is showing. :o I do try to shut up when I'm no 100% certain, and you caught me overstepping my bounds. It shows an image for me too, I had never tried fpassthru before!
In regards to the question, I don't know why it wouldn't be cached, I was under the impression that the cache will load pictures even if the pictures aren't called with html. At least that's how it used to work.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|