Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array as session variable

Is it possible to make an array a session variable in PHP?

The situation is that I have a table (page 1) with some cells having a link to a particular page. The next page will have a list of names (page 2, which I want to keep in a session array) with their respective checkboxes. On submitting this form, it will lead to a transaction page (page 3, where values of posted checkboxes are kept in a database for corresponding names). Now, if I return to the first page and click another cell, will the session array contain the new list of names or the old ones?

like image 405
anurag-jain Avatar asked Feb 21 '10 14:02

anurag-jain


People also ask

Can a session variable be an array?

Yes, PHP supports arrays as session variables.

How do you pass an array into a session variable?

session_start(); // start up your PHP session! $_SESSION['userdata'] = $userqty; print_r($_SESSION['userdata']); print_r($userqty;); $userqty_total=array_sum($userqty);

How do you create a session variable?

Before you can store any information in session variables, you must first start up the session. To begin a new session, simply call the PHP session_start() function. It will create a new session and generate a unique session ID for the user. The PHP code in the example below simply starts a new session.

How can store array in session in codeigniter?

$this->session->userdata('property_id') would return the entire array stored in the session for the 'property_id' variable. All you need to do after that is access the elements like a regular PHP array. Code: $value = $this->session->userdata('property_id');


1 Answers

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.

like image 141
Kaleb Brasee Avatar answered Oct 13 '22 22:10

Kaleb Brasee