为什么UnexpectedValueException
会在session_start()
中抛出?
我的对象具有 SPLObjectstorage
的属性。该对象被分配给 session ,如
$_SESSION['foo'] = $barObject;
我怀疑内部 session 序列化面临解码问题。我将 session 存储在数据库中,看起来它正在序列化 objectStorage
但无法对其进行解码。
示例 session 数据
self|O:4:"User":8:{s:5:"�*�id";N;s:7:"�*�nick";N;s:13:"�*�reputation";i:1;s:11:"�*�password";N;s:8:"�*�email";N;s:7:"�*�crud";O:10:"CRUDobject":2:{s:13:"�*�fieldCache";a:0:{}s:13:"�*�dependency";r:1;}s:7:"�*�auth";N;s:11:"�*�roleList";C:11:"RoleStorage":23:{x:i:1;N;,r:13;;m:a:0:{}}}
Rolestorage
是 SPLObjectstorage
的扩展
上面字符串的 session_decode()
也返回 false
有什么想法吗?
删除 roleList
属性可使其正确序列化。
如果我单独做
$sr = serialize($roles); // $roles is RoleStorage object
var_dump($sr);
var_dump(unserialize($sr));
它打印 string 'C:11:"RoleStorage":22:{x:i:1;N;,r:3;;m:a:0:{}}' (length=46)
然后在反序列化时失败并显示相同的消息。我不知道为什么会这样。
注意:在将对象附加到 RoleStorage
时,我使用对象本身作为数据。意味着它被存储为引用。我不知道 serialize()
如何(如果)在内部处理这个问题。
最佳答案
名称为 RoleStorage
的对象为我提出了几个标志。通常,此对象确实包含某种资源,或对内置 PHP 对象的引用。资源无法序列化,某些 PHP 内置类型也无法序列化。考虑在这些情况下实现神奇的 __sleep
和 __wakeup
方法。
假设您在 RoleStorage
对象中的某处有一个 PDO
引用,那么这些神奇的属性可能看起来像这样:
public function __sleep()
{
$this->pdo->commit();//commit && close
$this->pdo = array($dsn, $user, $pwd, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)
);
return serialize($this);
}
public function __wakeup()
{
$this->pdo = new PDO($this->pdo[0], $this->pdo[1], $this->pdo[2], $this->pdo[3]);
}
但既然你说 RoleStorage
对象是 SPLObjectStorage
的子对象,你最好覆盖 SPLObjectStorage
的实现the Serializable
interface :
It is not possible for __sleep() to return names of private properties in parent classes. Doing this will result in an E_NOTICE level error. Instead you may use the Serializable interface.
我建议在子类的 serialize
方法中声明遍历所有属性,并将该数据存储到一个数组中。返回序列化的数组,并在 unserialize
方法中反序列化该字符串,重新分配循环中的每个属性。
如果 SPLObjectStorage
具有私有(private)属性,您可以像这样访问它们:
class RoleStorage extends SPLObjectStorage
implements Serializable
{
public function serialize()
{
return serialize((array) $this);
}
public function unserialize($string)
{
$array = unserialize($string);
foreach($array as $property => $value)
{
$property = explode("\0", $property);//private & protected properties
$this->{end($property)} = $value;
}
}
}
有关explode("\0",$property);
的详细信息,请参阅manual , 或 check this question
关于php - session_start() php 中的 UnexpectedValueException 导致 SPLObjectStorage 序列化失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17962711/