Preloading images in a PHP session
I am using sessions to keep track of user data. I have many pages that access the same set of images. I开发者_如何学Pythons there a way to preload the images into a session so that they are loaded for the rest of the session?
-Mike
Browsers automatically keep a copy of images once they have been loaded the first time so they don't have to be downloaded again.
When your script starts, you can run the following:
if (!isset($_SESSION['images']))
{
$_SESSION['images'] = get_my_images();
}
Then, use $_SESSION['images']
whenever you need to access the set of images.
why do you want to keep image in the session?
you can serve resources like images with lighttpd or via CDN.
if you are talking about returning image links, you can cache the links with memcached etc.
On the first page after login/session establishment, just load all the images into a hidden div, at which point the browser will take care of cacheing them for you:
<div style="display: none">
<img src="img1.jpg" />
<img src="img2.jpg" />
...
</div>
There'll be a hit on the server for that first page load, but after either nothing for the duration of the browser cache, or at most a '304' check-if-newer type request.
精彩评论