如何从文件中反序列化 Symfony 会话? [英] How can I unserialize Symfony session from the file?

查看:39
本文介绍了如何从文件中反序列化 Symfony 会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Symfony 在 dev env 中的 app/cache/dev/sessions/sess_{session_id} 文件中存储会话.该文件的内容类似于:

Symfony store session in the app/cache/dev/sessions/sess_{session_id} file in dev env. The file's content is something like:

_sf2_attributes|a:0:{}_sf2_flashes|a:0:{}_sf2_meta|a:3:{s:1:"u";i:1396424236;s:1:"c";i:1396360957;s:1:"l";s:1:"0";}bbb|i:222;IsAuthorized|b:1;

当我尝试使用 unserialize() 函数反序列化它时 - 我得到 FALSE.

When I try to unserialize it with unserialize() function - I get FALSE.

我该如何取消序列化?

推荐答案

您可以只使用标准的 PHP 会话机制.您需要设置存储会话的目录 (app/cache/dev/sessions).然后调用标准函数 session_start() 将使用来自相应文件的所有未序列化数据填充 $_SESSION 变量.

You can just use standard PHP session mechanism. You need to set up the directory where your sessions is stored (app/cache/dev/sessions). And then calling standard function session_start() will fill the $_SESSION variable with all unserialized data from appropriate file.

例如您可以使用以下代码:

For example you can use this code:

ini_set('session.save_handler', 'files');
ini_set('session.save_path', 'path/to/your/site/folder/app/cache/dev/sessions');
session_start();

当您需要处理 Symfony 框架背后的会话时(根据 OP 需要),可以使用上述方式.要使用 Symfony 的会话机制,您应该使用 Session 对象,它将为您提供所有需要的信息:

The way described above can be used when you need to work with sessions behind Symfony framework (as OP needs). To use Symfony's session mechanism you should work with Session object that will provide you all the needed information:

use Symfony\Component\HttpFoundation\Session\Session;

$session = new Session();
$session->start();

$session->all(); // will return unserialized array of parameters

这篇关于如何从文件中反序列化 Symfony 会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆