Zend_Session:反序列化会话数据 [英] Zend_Session: unserialize session data
问题描述
我正在使用会话 SaveHandler
将会话数据保存在数据库中.
I'm using session SaveHandler
to persist session data in the database.
来自数据库的示例 session_data
列:
Sample session_data
column from the database:
Messenger|a:1:{s:13:"page_messages";a:0:{}}userSession|a:1:{s:7:"referer";s:32:"http://cms.dev/user/profile/view";}Zend_Auth|a:1:{s:7:"storage";O:19:"User_Model_Identity":3:{s:2:"id";s:1:"1";s:8:"username";s:13:"administrator";s:4:"slug";s:13:"administrator";}}
我想从这个会话数据中删除 Zend_Auth 对象.
I want to delete Zend_Auth object from this session data.
如何反序列化这些对象并删除我需要的对象?
How can I unserialize those objects and remove object I need?
我怀疑,我不必编写自定义解析器,Zend_Session
已经有一种方法可以做到这一点.我尝试了 unserialize
的不同组合,但它仍然返回 false
.
I suspect, that I don't have to write my custom parser, that Zend_Session
already has a method to do this. I have tried different combinations of unserialize
but it still returns false
.
我使用的是 ZF 1.10.2 和 Doctrine 1.2 中的自动加载器
I'm using autoloader from ZF 1.10.2 and Doctrine 1.2
推荐答案
下面的代码可以工作,它不是我的,但本质上它的作用是使用管道作为分隔符将会话字符串分开,反序列化单独分割块.
The code below will work, it is not mine, but in essence what it does is split the session string apart using the pipe as a delimiter, the unserialize the split chunks individually.
问题是php中unserialize函数的构建不理解串联的序列化.
The problem is that the build in unserialize function in php doesn't understand the concatenated serialization.
function unserialize_session_data( $serialized_string ) {
$variables = array();
$a = preg_split("/(\w+)\|/", $serialized_string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
for($i=0;$i<count($a);$i=$i+2){
$variables[$a[$i]] = unserialize($a[$i+1]);
}
return($variables);
}
这篇关于Zend_Session:反序列化会话数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!