Unserialize Error when offset 5 out of 9 bytes

I am trying to pass an array through the input field of an html form. Using serialization to pass it and then unserialize to read the array again. I have several input fields.

$test = array('name' => 'Sander', 'type' => 'melon'); echo '<input type="hidden" name="rank[]" value="'.serialize($test).'" >'; 

Then, if I want to non-sterilize it and show the data, it gives an error:

 $list = $_POST['rank']; var_dump($list); var_dump(unserialize($list[0])); 

enter image description here

+6
source share
4 answers

Instead of using serialize, I just use urlencode () and urldecode ().

Changed the array in a different format.

 $info = 'name=Sander&type=melon'; echo '<input type="hidden" name="rank[]" value="'.urlencode($info).'" >'; 

Then I can simply display the following values:

 if(!empty($_POST['rank'])){ $list = $_POST['rank']; $listSize = count($list); for($i=0;$i<$listSize;$i++){ parse_str(urldecode($list[$i]), $output); var_dump($output); } } 

The problem is resolved :)

0
source

Most likely you need to pass the serialized string through urlencode() before exiting.

To process it, use urldecode() to unserialize() .

+6
source

to try

  $list = urldecode($_GET['rank']); //var_dump($list); var_dump(unserialize($list)); $test = array('name' => 'Sander', 'type' => 'melon');?> <form > <input type='hidden' name='rank' value='<?php echo serialize($test);?>' > <input type="submit" > </form> 
+1
source

This is because when you add serialized data to html input, it creates an invalid html tag

 <input type="hidden" name="rank[]" value="a:2:{s:4:"name";s:6:"Sander";s:4:"type";s:5:"melon";}" > 

see the "Placements" section. In this regard, your mail data is incomplete

 var_dump($_POST['rank']); 

produces

 array(1) { [0]=> string(9) "a:2:{s:4:" } 

why aren't you trying to use json_encode and json_decode?

0
source

Source: https://habr.com/ru/post/943695/


All Articles