note 96504 added to function.serialize

From: Date: Tue, 02 Mar 2010 20:37:31 +0000
Subject: note 96504 added to function.serialize
Groups: php.notes 
Request: Send a blank email to php-notes+get-166544@lists.php.net to get a copy of this message
If serializing objects to be stored into a postgresql database, the 'null byte' injected for private and protected members throws a wrench into the system. Even pg_escape_bytea() on the value, and storing the value as a binary type fails under certain circumstances. For a dirty work around: <?php $serialized_object = serialize($my_object); $safe_object = str_replace("\0", "~~NULL_BYTE~~", $serialized_object); ?> this allows you to store the object in a readable text format as well. When reading the data back: <?php $serialized_object = str_replace("~~NULL_BYTE~~", "\0", $safe_object); $my_object = unserialize($serialized_object); ?> The only gotcha's with this method is if your object member names or values may somehow contain the odd "~~NULL_BYTE~~" string. If that is the case, then str_replace() to a string that you are guaranteed not to have any where else in the string that serialize() returns. Also remember to define the class before calling unserialize(). If you are storing session data into a postgresql database, then this workaround is an absolute must, because the $data passed to the session's write function is already serialized. Thanks, Travis Hegner http://www.travishegner.com/ ---- Server IP: 192.41.40.227 Probable Submitter: 71.13.65.130 ---- Manual Page -- http://www.php.net/manual/en/function.serialize.php Edit -- https://master.php.net/note/edit/96504 Del: integrated -- https://master.php.net/note/delete/96504/integrated Del: useless -- https://master.php.net/note/delete/96504/useless Del: bad code -- https://master.php.net/note/delete/96504/bad+code Del: spam -- https://master.php.net/note/delete/96504/spam Del: non-english -- https://master.php.net/note/delete/96504/non-english Del: in docs -- https://master.php.net/note/delete/96504/in+docs Del: other reasons-- https://master.php.net/note/delete/96504 Reject -- https://master.php.net/note/reject/96504 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#166544) next »