views:

63

answers:

2

Is it possible to make an array a session variable in php. The situation is I've a table(page 1) with some cells having a link to particular page. The next page will have a list of names(page 2 which I want to keep in session array) with their respective check box. on submitting this form which will lead to transaction page(page 3 here values of posted check boxes are kept in database for correspondin names). now if I return to the first page and clicks another cell, will the session array contain the new list of names or the old ones ??

+2  A: 

Yes, you can put arrays in sessions, example:

$_SESSION['name_here'] = $your_array;

Now you can use the $_SESSION['name_here'] on any page you want but make sure that you put the session_start() line before using any session functions, so you code should look something like this:

 session_start();
 $_SESSION['name_here'] = $your_array;

Possible Example:

 session_start();
 $_SESSION['name_here'] = $_POST;

Now you can get field values on any page like this:

 echo $_SESSION['name_here']['filed_name'];

As far second part of your question, the session variables remain there unless you assign different array data:

 $_SESSION['name_here'] = $your_array;

Session life time is set into php.ini file.

More Info Here

Sarfraz
what about the other part of my question..?
Anurag
@Anurag: I've updated my question for your second part of the question. Thanks :)
Sarfraz
Thanks a lot...
Anurag
@Anurag: You are welcome :)
Sarfraz
+2  A: 

Yes, PHP supports arrays as session variables. See this page for an example.

As for your second question: once you set the session variable, it will remain the same until you either change it or unset it. So if the 3rd page doesn't change the session variable, it will stay the same until the 2nd page changes it again.

Kaleb Brasee
can you please clear my doubt abt the later part of the question..?
Anurag
If you go back to the first page and click through to the second page (where the data is put into a session) the session data will contain a new set of data.
Glass Robot
Thanks I got it.. :)
Anurag