Pass an array from php to get

1

I have this array in php.

$error = array();
$error["error"] ="Tamaño máximo superado"

Before sending it I do this

$error = serialize($error);
$error = urldecode($error);
header("Location: subidaarchivos.php?".$error);

But when I get to the other page, it gets like this on the route.

Any solution?

    
asked by Borja Sanchez 18.05.2017 в 13:01
source

3 answers

1

The correct way to do what you want is:

<?php
/* En la captura de tu pregunta aparenta estar definido así 'error' */
$error = [ 'error' => '<li>Tamaño máximo superado</li>' ];
$error = serialize($error);
$error = urlencode($error);
/* OJO: agregamos 'mensaje=' para que en el otro lado llegue como $_GET['mensaje'] */
header("Location: subidaarchivos.php?mensaje=" . $error);

After converting the matrix $error in a string we must code it in URL with urlencode() to be able to add it to the URL later, but in your example you are decoding it with urldecode() .

And at the receiving end the work is simpler:

<?php
/* Comprobamos que ha llegado correctamente el campo 'error' */
if (isset($_GET['mensaje'])) {
    /* Deshacemos el trabajo hecho por 'serialize' */
    $error = unserialize($_GET['mensaje']);
    // El contenido del error está en el índice 'error'
    die($error['error']);
}

The data arrives correctly correctly formed to the content of the variable $_GET['mensaje'] , you only have to undo the work of serialize to access the data again with unserialize .

Finally, since it is a matrix, you must access the error index to be able to show the content of the error.

URL generated:

subidaarchivos.php?mensaje=a%3A1%3A%7Bs%3A5%3A%22error%22%3Bs%3A33%3A%22%3Cli%3ETama%C3%B1o+m%C3%A1ximo+superado%3C%2Fli%3E%22%3B%7D

Workaround hiding data in URL

You can easily hide the data that appears in the URL using, for example, base64_encode() .

<?php
/* En la captura de tu pregunta aparenta estar definido así 'error' */
$error = [ 'error' => '<li>Tamaño máximo superado</li>' ];
$error = serialize($error);
$error = base64_encode($error);
$error = urlencode($error);
/* OJO: agregamos 'error=' para que en el otro lado llegue como $_GET['mensaje'] */
header("Location: subidaarchivos.php?mensaje=" . $error);

And on the reception side:

<?php
/* Comprobamos que ha llegado correctamente el campo 'error' */
if (isset($_GET['mensaje'])) {
    /* Deshacemos el trabajo hecho por base64_encode */
    $error = base64_decode($_GET['mensaje']);
    /* Deshacemos el trabajo hecho por 'serialize' */
    $error = unserialize($error);
    // El contenido del error está en el índice 'error'
    die($error['error']);
}

Basically we introduce an additional step that encodes and decodes base64 the data that will appear in the URL, hiding its content or meaning at a glance:

URL generated:

subidaarchivos.php?mensaje=YToxOntzOjU6ImVycm9yIjtzOjMzOiI8bGk%2BVGFtYcOxbyBtw6F4aW1vIHN1cGVyYWRvPC9saT4iO30%3D
    
answered by 18.05.2017 / 13:15
source
1

When you receive it, you should do this, since you can not serialize the array and then not de-serialize it

<?php
 $error = unserialize($_GET["error"]); 
?>
    
answered by 18.05.2017 в 13:14
1

When sending

<?php
  $arrayError= Array(["error"]=>"Tamaño máximo superado");

  $urlPortion= '&error='.urlencode(serialize($arrayError));

  header("Location: subidaarchivos.php?".$urlPortion);?
?>

Upon receiving

<?php
    $receptor=unserialize($_GET['error']);
?>
    
answered by 18.05.2017 в 13:16