Which is the most secure way to send an array through POST
?
foreach ($id as $array) { <input type="hidden" name="prova[]" value="<?php echo $array; ?>"/> } <input type="submit" name="submit"/>
or using implode()
to create a single variable, pass the variable and then use explode()
to get back the values into a new array?
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);
Use the POST Method to Send an Array From a Form in PHP. We use the POST method to send data from a form in PHP. The POST method is an HTTP request method that creates or adds resources to the server. We use it when we have to send sensitive information like passwords.
To post an array from an HTML form to PHP, we simply append a pair of square brackets to the end of the name attribute of the input field. For example: <form method="post"> <input name="favorites[]" type="text"/>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With