[SOLVED] Unserialize parameter problem

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
nervino
Power Poster
Power Poster
Posts: 448
Joined: Sun Dec 28, 2008 12:15 pm
Location: Roma, Italy

[SOLVED] Unserialize parameter problem

Post 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
Last edited by nervino on Mon Oct 11, 2010 5:51 pm, edited 1 time in total.
Jos
Support Guru
Support Guru
Posts: 4019
Joined: Wed Sep 05, 2007 8:03 pm
Location: The Netherlands

Re: Unserialize parameter problem

Post 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']));
nervino
Power Poster
Power Poster
Posts: 448
Joined: Sun Dec 28, 2008 12:15 pm
Location: Roma, Italy

Re: Unserialize parameter problem

Post 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']).
Last edited by nervino on Mon Oct 11, 2010 3:37 pm, edited 1 time in total.
nervino
Power Poster
Power Poster
Posts: 448
Joined: Sun Dec 28, 2008 12:15 pm
Location: Roma, Italy

Re: Unserialize parameter problem

Post 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.
Post Reply

Return to “Developers Discussion”