Page 1 of 1

[SOLVED] Unserialize parameter problem

Posted: Sun Oct 10, 2010 6:55 pm
by nervino
Hi, I'm trying to pass an array in an hidden field, serializing it, but I got nothing when I unserialize it.

I serialize a parameter and pass it in an hidden field,  in this way:
$arr_hidden_ids=base64_encode(serialize($arr_id));
 (where "$arr_id" is an array of id).

Pass the hidden field to Smarty:
$this->smarty->assign('hidden_ids', $this->CreateInputHidden($id, 'hidden_ids[]',$arr_hidden_ids ));
In the source of the generated page I have:
then, I decode and unserialize the parameter when receiving the submitted form:
$arr_hidden_ids=base64_decode(unserialize($params['hidden_ids']));
print_r($arr_hidden_ids);
It prints nothing.


If I don't decode/unserialize,
$arr_hidden_ids=$params['hidden_ids'];
print_r($arr_hidden_ids);
It prints:
Array ( [0] => YToyOntpOjA7czoyOiIxMCI7aToxO3M6MToiOSI7fQ== )
Why? What am I doing wrong?

Thanks

Re: Unserialize parameter problem

Posted: Sun Oct 10, 2010 9:15 pm
by Jos
Maybe this

Code: Select all

$arr_hidden_ids=unserialize(base64_decode($params['hidden_ids']));
in stead of

Code: Select all

$arr_hidden_ids=base64_decode(unserialize($params['hidden_ids']));

Re: Unserialize parameter problem

Posted: Mon Oct 11, 2010 2:24 pm
by nervino
I tried your suggestion but $arr_hidden_ids is still empty after decode..

With debug=true in config.php, I receive this warning:
Warning: base64_decode() expects parameter 1 to be string, array given in (...where I try to unserialize the $params['hidden_ids']).

Re: Unserialize parameter problem

Posted: Mon Oct 11, 2010 5:51 pm
by nervino
I have found the trick:
// When unserializing/decoding
$arr_hidden_ids=unserialize(base64_decode($params['hidden_ids']));

// The variable used for query the db, has to be unserialized/decoded again. So I've created a new one
$arr_hidden_ids_FOR_QUERY=unserialize(base64_decode($arr_hidden_ids));

//Pass to Smarty
$this->smarty->assign('hidden_ids', $this->CreateInputHidden($id, 'hidden_ids',base64_encode(serialize($arr_hidden_ids)) ));
Thank you Jos, You put me on the right way.