In PHP, unserialize() turns a string created by serialize() back into a PHP value. It can rebuild integers, strings, arrays and even objects.
Security warning: never call unserialize() on data that comes from a user, a cookie or any untrusted place. Crafted data can create objects of any class and trigger their magic methods, which attackers use to run code. For untrusted data, use json_decode(), or at least pass the allowed_classes option.
Syntax
Call unserialize() with the values you need. Start with this line.
Returns the integer 42. Only use this with data you created yourself.
unserialize($data, ['allowed_classes' => false])
Safer. Objects are not created and become __PHP_Incomplete_Class instead.
unserialize('not valid')
Returns false and raises a notice or warning because the string is not valid serialized data.
Always pass allowed_classes with false or a short list of class names. Better still, store data as JSON and read it with json_decode(), which cannot create objects of arbitrary classes.
Required. The serialized string, usually created by serialize(). Treat any string from outside your code as untrusted.
options
Optional. An array of options. allowed_classes can be false (no objects), true (any class, the unsafe default), or an array of class names. max_depth sets how deep nested data may go, and the default is 4096.
Return Value
Return Value
Description
mixed
The original value. It returns false if the string is invalid, but the valid data b:0; also gives false, so compare with serialize(false) when you need to be sure.
PHP Examples
Example 1 — Restoring an Array
Serialize an array that your own code created, then read it back. This is the safe use of unserialize().
serialize() creates the string. The data comes from our own code, so we can trust it.
echo shows the stored format. It lists each key and value with its type and length.
unserialize() rebuilds the array. $user is a normal PHP array again.
We can read items as usual. $user['name'] gives Asha and $user['age'] gives 25.
Example 2 — Failure Versus a Real false
unserialize() returns false for bad input, but false is also a valid value. Check carefully, and use the @ sign here only to hide the PHP notice for this demo.
PHP
<?php
$input = 'this is not serialized';
$result = @unserialize($input);
if ($result === false && $input !== serialize(false)) {
echo 'Invalid data', PHP_EOL;
}
var_dump(unserialize('b:0;'));
Output
Invalid data
bool(false)
How It Works
The input is plain text. It is not in the serialized format, so unserialize() fails and returns false.
The @ sign hides the warning. In real code, log the problem instead of hiding it.
Comparing with serialize(false) separates failure from a real false. Our text does not match b:0;, so it is invalid.
The string b:0; is valid. It restores the boolean false, so no error is shown.
Example 3 — Limiting Classes with allowed_classes
Serialize an object, then restore it twice: once with objects blocked, and once with only the Point class allowed.
PHP
<?php
class Point {
public $x = 1;
public $y = 2;
}
$data = serialize(new Point());
$safe = unserialize($data, ['allowed_classes' => false]);
echo get_class($safe), PHP_EOL;
$trusted = unserialize($data, ['allowed_classes' => ['Point']]);
echo get_class($trusted), PHP_EOL;
echo $trusted->x + $trusted->y, PHP_EOL;
Output
__PHP_Incomplete_Class
Point
3
How It Works
We serialize a Point object. The string records the class name Point.
With allowed_classes set to false, no real object is made. PHP creates a harmless __PHP_Incomplete_Class instead.
With ['Point'], only that class is allowed. The result is a real Point object.
The trusted object works normally. x plus y gives 3.
Frequently Asked Questions
It takes a string made by serialize() and turns it back into a PHP value, such as an array or an object. It returns false if the string is not valid serialized data.
No. Unserializing untrusted data can lead to PHP object injection and even remote code execution. Never pass cookies, form fields or other user data to it. Use json_decode() instead.
It controls which classes unserialize() may create. Set it to false to block all objects, or to an array of class names to allow only those. Blocked objects become __PHP_Incomplete_Class.
unserialize() returns false for both. Compare the input with serialize(false), which is 'b:0;'. If they do not match and the result is false, the data was invalid.
Use JSON for data that crosses a trust boundary. json_encode() and json_decode() only produce plain arrays and objects, so they cannot trigger the magic methods of your classes.