Joshua Merriman
Joshua Merriman

Reputation: 985

php - Passing arrays to $_POST

I'm working on a PHP app that has one (of many) arrays vital to the operation of the program. I need to keep the contents of this array intact in between page loads. I know that $_POST can be used to retain most data types over page loads, but how does one put an array... In an array?

The array is a simple one, but the amount of indices it has depends on user input - it can range from anywhere between 1 to 50.

Is there any way I can retain an array's data between page loads with $_POST?

Upvotes: 0

Views: 1403

Answers (2)

SeanWM
SeanWM

Reputation: 16989

Your question isn't really clear. Passing variables between pages should be held in a $_SESSION not a $_POST variable. Storing an array in a session variable is the same as saving any variable to a session variable.

$session_start();
$_SESSION['my_array'] = array('one', 'two', 'three');
foreach($_SESSION['my_array'] as $value) {
    echo $value;
}

Upvotes: 3

Dai
Dai

Reputation: 155015

Rule #1 in client/server development: Never trust the client.

You're better off storing the array in PHP's session state, or serializing it out to disk.

Fortunately, it's easy:

$_SESSION['someKey]' = $myArray;

There is another problem with your proposal of storing it in the page: what happens if the user uses the Back button and resubmits or otherwise breaks your page-ordering? By storing state on the client you cannot rely on it anymore.

Upvotes: 0

Related Questions