is possible to store an array in a session (session_start) ?

I could do a basic shopping cart with an array of two dimensions (quantity and product code) so I wonder if is possible to store an array in a session (session_start) and how ?

Regards
dimensionavAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

eriksmtkaCommented:
yes, it is possible to store an array in a session variable.  and the process is no different than any other variable.

session_start();
$_SESSION['some_array'] = array(array('a', 'b', 'c'), array(1, 2, 3));
var_dump($_SESSION['some_array'];

as for any variable stored as a session variable, all you have to do is store it in the "Session Variable Array".
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
eriksmtkaCommented:
not being able to edit comments can be a royal pain sometimes...

forgot the closing ')' on the var_dump function
0
Graceful_PenguinCommented:
The $_SESSION variable is just like a normal associated array, this array is just special in the way that the session mechanism interacts with it.

Each request to the server is designed to be independent of all other request. This works great for most pages but sometimes you want to know which user is requesting the resource. This is possible with browser cookies, which are basically just strings that the browser sends with every request to your domain or path to identify it self or the user.

So now PHP sees this cookie and looks if there is a session associated with that cookie. If there is it loads the data associated with that session and plugs it into the $_SESSION array. Also when it has finished a request it checks to see if there is any changes to the data and saves the changes.

You can even change where and how this data of each session is stored but that is beyond the scope of this question.

So I hope that gives you a better understanding of how sessions and cookies work.
0
Ray PaseurCommented:
You can use serialize() to turn the array into a string, and use unserialize() to turn the string back into an array.  The session handler will be fine with these things.  But there is a more important issue, and that is the expiration of the session.

PHP sessions expire at the end of the life of the browser window.  Poof, you've lost communication with a potential client.  

Instead, why not grab all of the information you can get about the client (IP address, items browsed, etc.) and create a row in a permanent data base table of potential clients?  Then create a unique identifier and put a cookie on the client's browser with a key to all the client information you could harvest?  This is the way professional web sites do it.  Visit any online market.  Don't bother to sign in -- just look at some stuff.  Then close your browser window and come back to the site "tomorrow."  They will welcome you with open arms and suggestions about products that you might like to consider.  Other sites will know what you searched for!

It's not really a computer science problem -- it's a sales psychology problem.  Us computer science geeks are just the guys that provide the solutions.

Best regards, and Happy New Year, ~Ray
0
dimensionavAuthor Commented:
Happy new year for you too!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.