Currently, to check if something is serialized, I do this:

function is_serialized($data)
    return (@unserialize($data) !== false);

But it issues an E_NOTICE. The @ is great to suppress output, but it's still clogging up every other line of my error log (which is purposefully enabled for notices). Are there any cleaner alternatives??

Recommended Answers

All 3 Replies

FALSE is returned both in the case of an error and if unserializing the serialized FALSE value. It is possible to catch this special case by comparing str with serialize(false) or by catching the issued E_NOTICE.

Do not pass untrusted user input to unserialize(). Unserialization can result in code being loaded and executed due to object instantiation and autoloading, and a malicious user may be able to exploit this. Use a safe, standard data interchange format such as JSON (via json_decode() and json_encode()) if you need to pass serialized data to the user.

Quote from manual

Will work on it and let you know to supresss E_NOTICE other than using @

As per what i have digged out ,you are doing it in proper way except for use base64_decode($data) instead of $data.

Another way to do the same is to use regular expression instead of calling unserialize() method as the data retured after serialization returns particular format.
For String like :
For Array like

 function mb_unserialize($serial_str) {  
$out = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $serial_str );  
return unserialize($out);  


$error_reporting = error_reporting(error_reporting() ^ E_NOTICE);

$y = unserialize("sdfsdf");
if($y) {
   echo $y;
else {
    // do something else


By this you can change error_reporting .It says that report error for all except E_NOTICE.

I am not sure whether this can be configured for a page only.
Please do let me know if error_reporting can be changed from page to page if required.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.