In PHP, is it possible to set an array as a session variable?
The situation is that I have a table (first page) in which some cells have links to specific pages. The next page will have a list of names (the second page, which I want to save in a session array) with corresponding checkboxes. After submitting this form, it will lead to a transaction page (third page) where the value of the published checkbox will be saved in the database under the corresponding name. Now, if I go back to the first page and click on another cell, will the sessions array contain the new list of names or the old list of names?
Yes, you can put an array into the session, for example:
Now you can use
$_SESSION['name_here']
on any page, but before using any session function, make sure to add thesession_start()
line to your code, So your code should look like:Possible examples:
Now you can get the field value on any page like this:
As for the second part of your question, unless you allocate different array data, the session variables will remain there:
The session lifetime is set in thephp.inifile.
For more information please click here
Yes, PHP supports arrays as session variables. Please refer tothis pagefor examples.
As for your second question: Once a session variable is set, it will remain the same unless you change it or
unset
it. So if the third page doesn't change the session variable, it will remain the same as it was before the second page changed it.