views:

46

answers:

4

Hi,

I am using sessions to keep track of user data. I have many pages that access the same set of images. Is there a way to preload the images into a session so that they are loaded for the rest of the session?

-Mike

A: 

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.

Victor Nicollet
Please explain the negative rating... how else would you store a list of images in a session?
Victor Nicollet
A: 

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.

Andy Lin
+1  A: 

Browsers automatically keep a copy of images once they have been loaded the first time so they don't have to be downloaded again.

Matt Williamson
This isn't necessarily true. You need to make sure your images' headers have the proper cache-control set, otherwise the browser won't cache them. You can use Firebug or the inspector in Chrome to check the HTTP headers.
Josh Smith
A: 

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.

Marc B