Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP, pass array through POST

Tags:

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?

like image 820
user1722791 Avatar asked Dec 28 '12 15:12

user1722791


People also ask

How to pass array in POST?

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);

Can you post an array PHP?

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.

How do you post an array?

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"/>


1 Answers

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.

like image 83
MrGlass Avatar answered Nov 09 '22 23:11

MrGlass