The steps you must follow to send a form field in HTML with serialized data from PHP are:
The best combination is serialize()
/ base64_encode()
/ <textarea>
(see the last example).
serialize()
/ htmlspecialchars()
/ <input>
In this example I use serialize()
, htmlspecialchars()
and the label <input>
(just like you tried to do yourself):
<?php
$resultado = [
/* Si aquí ponemos "valor<pre><?php
var_dump(unserialize($_POST['resultado']));
?></pre>
hola" falla la decodificación */
1 => "valorhola",
'hola' => [
'uno' => 'dos',
'tres' => 3,
],
];
$datos = serialize($resultado);
?><form action="pruebaExportar.php" method="post">
<input type="hidden" name="resultado"
value="<?= htmlspecialchars($datos) ?>" >
<input type="submit" value="word">
</form>
On the decode side:
<?php
$resultado = [
1 => "valor<pre><?php
var_dump(json_decode($_POST['resultado']));
?></pre>
hola",
'hola' => [
'uno' => 'dos',
'tres' => 3,
],
];
$datos = json_encode($resultado);
?><form action="pruebaExportar.php" method="post">
<input type="hidden" name="resultado"
value="<?= htmlspecialchars($datos) ?>" >
<input type="submit" value="word">
</form>
Problems:
-
unserialize()
/ htmlspecialchars()
may fail in the decoding of strings with binary content (images, etc) due to the treatment made by the browser / server of the form fields.
json_encode()
/ htmlspecialchars()
/ <input>
In this example I use json_encode()
, htmlspecialchars()
and the label <input>
:
<?php
$resultado = [
1 => "valor<pre><?php
var_dump(unserialize(base64_decode($_POST['resultado'])));
?></pre>
hola",
'hola' => [
'uno' => 'dos',
'tres' => 3,
],
];
$datos = serialize($resultado);
?><form action="pruebaExportar.php" method="post">
<textarea style="display: none;" name="resultado"><?=
base64_encode($datos) ?></textarea>
<input type="submit" value="word">
</form>
On the decode side:
<?php
$resultado = [
/* Si aquí ponemos "valor<pre><?php
var_dump(unserialize($_POST['resultado']));
?></pre>
hola" falla la decodificación */
1 => "valorhola",
'hola' => [
'uno' => 'dos',
'tres' => 3,
],
];
$datos = serialize($resultado);
?><form action="pruebaExportar.php" method="post">
<input type="hidden" name="resultado"
value="<?= htmlspecialchars($datos) ?>" >
<input type="submit" value="word">
</form>
Problems:
-
json_encode()
can change data types Array
to Object
by having non-numeric indexes.
serialize()
/ base64_encode()
/ <textarea>
In this example I use serialize()
, base64_encode()
and the label <textarea>
:
<?php
$resultado = [
1 => "valor<pre><?php
var_dump(json_decode($_POST['resultado']));
?></pre>
hola",
'hola' => [
'uno' => 'dos',
'tres' => 3,
],
];
$datos = json_encode($resultado);
?><form action="pruebaExportar.php" method="post">
<input type="hidden" name="resultado"
value="<?= htmlspecialchars($datos) ?>" >
<input type="submit" value="word">
</form>
On the decode side:
<?php
$resultado = [
1 => "valor<pre><?php
var_dump(unserialize(base64_decode($_POST['resultado'])));
?></pre>
hola",
'hola' => [
'uno' => 'dos',
'tres' => 3,
],
];
$datos = serialize($resultado);
?><form action="pruebaExportar.php" method="post">
<textarea style="display: none;" name="resultado"><?=
base64_encode($datos) ?></textarea>
<input type="submit" value="word">
</form>
Any binary data will be coded in base64
, normal characters, so there will be no problems when decoding data, no matter how the browser or the server treats the form field.
Problems:
- The
base64
encoding generates four ASCII characters in its output (6 bits / character x 4 characters = 24 bits ) for every three input characters (8 bits / character x 3 characters = 24 bits). In other words, it increases the size of the data sent by 33% (plus the characters% co_of filling% of the last remaining bits until completing the four output characters, practically negligible).