PHP Serialize & Unserialize -
i have problem unserialize return nothing after getting $_post. post simple array form , see double quotes been added serialized string. tried stripslashes before serialzing , before unserializing, result same no output. have turned of magic_quotes_gpc in php.ini , again no luck. when hardcode values in unserialize adding "\" manually gave me desired string, tried addslashes , again left nothing. please me resolve this.
<?php $tmp = $_post["strvid"]; $mynewarray = unserialize($trp); print_r($mynewarray); $myarray = array('key1'=>'value1', 'key2'=>'value2'); $serialized = serialize($myarray); ?> <form onsubmit="return validate(this)" action="test_serialize.php" method="post"> <input type="hidden" name="strvid1" value="this mega shit"/> <input type="hidden" name="strvid" value="<?php echo $serialized; ?>"> <input type="submit" name="next" value="next"/> </form>
updated...
after serialize: a:2:{s:4:"key1";s:6:"value1";s:4:"key2";s:6:"value2";}
after stripslashes of serialize: a:2:{s:4:"key1";s:6:"value1";s:4:"key2";s:6:"value2";}
when add \ manually
$mynewarray = unserialize("a:2:{s:4:\"key1\";s:6:\"value1\";s:4:\"key2\";s:6:\"value2\";} ");
i output array ( [key1] => value1 [key2] => value2 )
thanks
in code:
<input type="hidden" name="strvid" value="<?php echo $serialized; ?>">
that's wrong, because variable contain double quotes; must escape those:
<input type="hidden" name="strvid" value="<?php echo htmlspecialchars($serialized, ent_quotes, 'utf-8'); ?>">
btw, assuming you're doing unserialize it:
$mynewarray = unserialize($_post["strvid"]);
Comments
Post a Comment