Storing pictures in sessions?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bugrush
Forum Newbie
Posts: 15
Joined: Tue Dec 16, 2008 3:00 pm

Storing pictures in sessions?

Post by bugrush »

A quick question: Is it good idea to store images in sessions? Are all session variables loaded from session files into memory immediately when script starts? If they aren't then it might be a good idea? If they are, I'll use database instead. I need a good place to store some temporary stuff - many kinds of stuff. I don't want that temporary data to be scattered around in files, database and sessions.
bugrush
Forum Newbie
Posts: 15
Joined: Tue Dec 16, 2008 3:00 pm

Re: Storing pictures in sessions?

Post by bugrush »

so? waht about php sessions? Are all the session variables loaded from session files into the memory immediately everytime you call session_start?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Storing pictures in sessions?

Post by John Cartwright »

It is a *very* bad idea to store any binary in the session files. I wouldn't put them in the database either. Both of which cause unneccesary overhead (especially the former).

What is wrong with putting them in a central location on the file system, and if need be, store the meta data in the database.
bugrush
Forum Newbie
Posts: 15
Joined: Tue Dec 16, 2008 3:00 pm

Re: Storing pictures in sessions?

Post by bugrush »

I know it will cause some overhead, because its a filesystem inside a filesystem, but it's temporary data(max 1-2mb). Files feel a bit messy. Ok, but what about session variables? How are they loaded into memory? I mean if I have a lot of data in sessions and I use only small part of it on every request.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Storing pictures in sessions?

Post by s.dot »

There is no need (ever, i believe) to store image data in a session or in a database. It's already on the server anyways in /tmp.. you can leave it there to be picked up by garbage collection or move it to a permanent location. Reference the file location in the session variable instead.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
bugrush
Forum Newbie
Posts: 15
Joined: Tue Dec 16, 2008 3:00 pm

Re: Storing pictures in sessions?

Post by bugrush »

Ok, I'll do that. Storing files in sessions(or in database) was just a random idea. Never done that before. I was just wondering if it was thinkable to store something bigger in sessions. I'll ask again: How is the session loaded? Does php dump whole session into the memory every time or are the necessary session vars loaded only when used?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Storing pictures in sessions?

Post by s.dot »

Here's my guess:

When session_start() is initialized.. the session file (yes it is a flat file) is read and unserialize()'d and it is all loaded into memory. Then all session variables are available throughout the entire script.

That's what I would think, but I have not researched session usage extensively.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Storing pictures in sessions?

Post by josh »

That's correct. If you just unset() the data on the next request there'd be not a lot of extra overhead, but I'm gonna say you should store on filesystem, much better. If you made a re-usable class it wouldn't be as messy as you think it seems.
Post Reply