PHP, pass array through POST PHP, pass array through POST arrays arrays

PHP, pass array through POST


Edit If you are asking about security, see my addendum at the bottom Edit

PHP has a serialize function provided for this specific purpose. Pass it an array, and it will give you a string representation of it. When you want to convert it back to an array, you just use the unserialize function.

$data = array('one'=>1, 'two'=>2, 'three'=>33);$dataString = serialize($data);//send elsewhere$data = unserialize($dataString);

This is often used by lazy coders to save data to a database. Not recommended, but works as a quick/dirty solution.

Addendum

I was under the impression that you were looking for a way to send the data reliably, not "securely". No matter how you pass the data, if it is going through the users system, you cannot trust it at all. Generally, you should store it somewhere on the server & use a credential (cookie, session, password, etc) to look it up.


http://php.net/manual/en/reserved.variables.post.php

The first comment answers this.

<form ....><input name="person[0][first_name]" value="john" /><input name="person[0][last_name]" value="smith" />...<input name="person[1][first_name]" value="jane" /><input name="person[1][last_name]" value="jones" /></form><?phpvar_dump($_POST['person']);array (0 => array('first_name'=>'john','last_name'=>'smith'),1 => array('first_name'=>'jane','last_name'=>'jones'),)?>

The name tag can work as an array.


You could put it in the session:

session_start();$_SESSION['array_name'] = $array_name;

Or if you want to send it via a form you can serialize it:

<input type='hidden' name='input_name' value="<?php echo htmlentities(serialize($array_name)); ?>" />$passed_array = unserialize($_POST['input_name']);

Note that to work with serialized arrays, you need to use POST as the form's transmission method, as GET has a size limit somewhere around 1024 characters.

I'd use sessions wherever possible.