Page 1 of 1
Storing pictures in sessions?
Posted: Wed Feb 11, 2009 5:44 pm
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.
Re: Storing pictures in sessions?
Posted: Thu Feb 12, 2009 12:02 pm
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?
Re: Storing pictures in sessions?
Posted: Thu Feb 12, 2009 12:19 pm
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.
Re: Storing pictures in sessions?
Posted: Thu Feb 12, 2009 12:33 pm
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.
Re: Storing pictures in sessions?
Posted: Thu Feb 12, 2009 12:39 pm
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.
Re: Storing pictures in sessions?
Posted: Thu Feb 12, 2009 12:57 pm
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?
Re: Storing pictures in sessions?
Posted: Thu Feb 12, 2009 1:03 pm
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.
Re: Storing pictures in sessions?
Posted: Thu Feb 12, 2009 1:35 pm
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.