unserialize values

Last week I had the problem, that – due to a wrong database-entry – a serialized value got scrambled. And due to that (and some missconfiguration of the server) the application broke. Instead of a search result the user simply got an error message about not being able to unserialize the value due to an offset-error.

So how could I avoid the error (which actually is a notice) being thrown and besides that get to know whether unserializing worked or not?

The hack I finaly came up with is as follows:

  • suppress the notice in any way
  • check whether the returned value is a boolean false and the given string is ‘b:0;’ (which represents a boolean false in a serialized string).
  • If so, the unserialize() failed, so lets throw an InvalidArgumentException

And here it is:

public function my_unserialize ( $value )
{
    $unserialized = @unserialize ( $value );
    if ( false === $unserialized && 'b:0;' !== $value ) {
        $le = error_get_last ();
        throw new InvalidArgumentException ( $le['message'] );
    }
    return $unserialized;
}


 
 architectural daughtsman, brother, developer, father, husband, master of forestry sciences, scout


2 Gedanken zu „unserialize values

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>