[SOLVED]storing session arrays design advice

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
qwanta
Forum Newbie
Posts: 4
Joined: Thu Sep 14, 2006 9:18 am

[SOLVED]storing session arrays design advice

Post by qwanta »

Hi,
My question is about storing large arrays and passing them between pages. A simple session array doesn't work because a user needs one array per tab/window he is looking at. Basically I need an equivalent to having one session_id for each tab/window.

To give a little more detail:
I am getting data from a mysql database and displaying it in a graph (jpgraph).

Once the graph is displayed, I have buttons to allow the user to zoom in, adjust scale/colors etc of graph. Because of the nature of the data, it is much more efficient to store the graph data in an array rather than requery it when these graph adjustments are made (the underlying data of course doesn't change).
The user can have as many of these graphs open in windows/tabs as he wishes, so one array needs to be stored for each window/tab.

I've hit upon 2 ways to do this so far each with its shortcomings.
1) fwrite the data to files (filename based on timestamps & session id), 1 file per graph - probably have a separate script to clean out files older than 3 days or so. The shortcoming here is that the deletion of files is not very clean, ideally I would like to delete them immediately when the window/tab graph is closed.
2) Create multiple session arrays on the fly as needed. The problem I see here is that all the arrays are written to the same file which doesn't seem to be a good approach - 1 file per graph seems to make more sense.

What would be the recommended/best practice way you would do this?

Thanks
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Re: storing session arrays design advice

Post by patrikG »

qwanta wrote:Basically I need an equivalent to having one session_id for each tab/window.
That's not necessary and, besides, quite impossible. The $_SESSION-superglobal can store n-dimensional arrays (although I would advise not using more than 2-dimensional arrays - gets to complicated otherwise).
qwanta wrote: I've hit upon 2 ways to do this so far each with its shortcomings.
1) fwrite the data to files (filename based on timestamps & session id), 1 file per graph - probably have a separate script to clean out files older than 3 days or so. The shortcoming here is that the deletion of files is not very clean, ideally I would like to delete them immediately when the window/tab graph is closed.
Serialise the data you need to safe and store it in the database. AJAX would be a very good solution there (I'd recommend xajax) as it would allow you to via Javascript's "onUnload"-event to delete the respective row in the database.
qwanta
Forum Newbie
Posts: 4
Joined: Thu Sep 14, 2006 9:18 am

Post by qwanta »

Thanks for the serialize tip, I haven't used that kind of thing before. I think logging serialized data to mysql with a timestamp field will work great - I'll just delete records older than a few days automatically everytime I access the database.

thanks again
Post Reply