Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP filter unserialize()

PHP filter unserialize()

PHP 7 New Features PHP 7 New Features

PHP 7 added the ability to provide filtering for unserialize(), which can prevent illegal data from performing code injection and provides safer deserialized data.

Example

Example

obj1prop = 1 ; $obj2 = new MyClass2 (); $obj2 -> obj2prop = 2 ; $serializedObj1 = serialize ( $obj1 ); $serializedObj2 = serialize ( $obj2 ); // default behavior is to accept all classes // the second parameter can be ignored // if allowed_classes is set to true, unserialize will convert all objects to __PHP_Incomplete_Class objects $data = unserialize ( $serializedObj1 , [ "allowed_classes" => true ]); // convert all objects to __PHP_Incomplete_Class objects, only allow MyClass1 and MyClass2 to convert to __PHP_Incomplete_Class $data2 = unserialize ( $serializedObj2 , [ "allowed_classes" => [ "MyClass1" , "MyClass2" ]]); print( $data -> obj1prop ); print( PHP_EOL ); print( $data2 -> obj2prop ); ?>

The output of the above program is:



1

2


PHP 7 New Features PHP 7 New Features