note 110553 added to function.unserialize

From: Date: Tue, 06 Nov 2012 08:37:54 +0000
Subject: note 110553 added to function.unserialize
Groups: php.notes 
Request: Send a blank email to php-notes+get-191780@lists.php.net to get a copy of this message
When you meet '__PHP_Incomplete_Class_Name' by using unserialize , please confirm whether you have included the class file in serialized contents before you use unserialize function. For example: Include class <?php /*SimpleClass.php*/ class SimpleClass{ var $key; function SimpleClass(){ } function __set($name,$value){ $this->key[$name] = $value; } function __get($name){ return $this->key[$key]; } } ?> Write file <?php /*Store.php */ require_once './SimpleClass.php'; $SimpleClass = new SimpleClass; $SimpleClass->keywords = 'Google'; @file_put_contents('./aaa.txt', serialize($SimpleClass)); ?> Wrong Read file <?php /*Read.php */ $SimpleClass = unserialize(file_get_contents('./aaa.txt')); print_r($SimpleClass); ?> Wrong Output __PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => SimpleClass [key] => Array ( [keywords] => Google ) ) In order to resolve this problem, you should include the class file in serialized contents before you use unserialize function. Correct Read File <?php /*Read.php */ require_once './SimpleClass.php'; $SimpleClass = unserialize(file_get_contents('./aaa.txt')); print_r($SimpleClass); ?> Correct Output SimpleClass Object ( [key] => Array ( [keywords] => Google ) ) ---- Server IP: 69.147.83.199 Probable Submitter: 58.81.201.106 ---- Manual Page -- http://www.php.net/manual/en/function.unserialize.php Edit -- https://master.php.net/note/edit/110553 Del: integrated -- https://master.php.net/note/delete/110553/integrated Del: useless -- https://master.php.net/note/delete/110553/useless Del: bad code -- https://master.php.net/note/delete/110553/bad+code Del: spam -- https://master.php.net/note/delete/110553/spam Del: non-english -- https://master.php.net/note/delete/110553/non-english Del: in docs -- https://master.php.net/note/delete/110553/in+docs Del: other reasons-- https://master.php.net/note/delete/110553 Reject -- https://master.php.net/note/reject/110553 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#191780) next »