如何使用symfony2正确地重新打开会话? [英] How to correctly reopen a session using symfony2?
问题描述
我在我的symfony2应用程序中使用了一个flash组件,它可以上传多个图像,当它们到达服务器时,我想重新打开会话,以便可以将图像存储在相对于登录用户的位置。我发送每个文件上传的PHP会话ID ...
在香草PHP中,我可以实现这一点...
session_id($ originalSessionId);
session_start();
但是有没有symfony2使用安全上下文的方法?
编辑:这是一个类似的问题,尽管我想通过传递一个令牌来创建某种基于令牌的登录,并通过一个单独的防火墙来保护上传的URL。
首先,您需要创建自己的会话存储类,如下所示: <?php
命名空间Elao \ BackBundle \ Session;
使用Symfony \ Component \DependencyInjection\ContainerInterface;
使用Symfony \ Component \HttpFoundation\SessionStorage\NativeSessionStorage;
$ b $ class存储扩展NativeSessionStorage
{
public function __construct(array $ options = array(),ContainerInterface $ container)
{
$ request = $容器 - >获得( '请求'); ($ session),
if($ request-> query-> has('sessionId')){
$ request-> cookies-> set(session_name(),1) //为了绕过hasPreviousSession安全检查,我们必须模拟这个cookie
session_id($ request-> query-> get('sessionId'));
}
return parent :: __ construct($ options);
$ b然后,你必须重新定义默认的(在你的例如config.yml:
$ $ p $ 参数:
session.storage.native.class:ElaoBackBundleSessionStorage
服务:
session.storage.native:
class:%session.storage.native.class%
参数:[%session.storage.options%,@service_container]
I'm using a flash component in my symfony2 application which uploads multiple images, and when they reach the server I want to re-open the session so that I can store the image in a location relative to the logged in user. I am sending the PHP session ID with each file upload...
In vanilla PHP I can achieve this along the lines of...
session_id($originalSessionId);
session_start();
but is there a symfony2 way of doing this using the security context?
EDIT: This is a similar question, although I want to maybe create some kind of token based login by passing a token and securing the upload URL with a separate firewall.
First, you need to create your own session storage class like the following:
<?php
namespace Elao\BackBundle\Session;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage;
class Storage extends NativeSessionStorage
{
public function __construct(array $options = array(), ContainerInterface $container)
{
$request = $container->get('request');
if ($request->query->has('sessionId')) {
$request->cookies->set(session_name(), 1); // We have to simulate this cookie, in order to bypass the "hasPreviousSession" security check
session_id($request->query->get('sessionId'));
}
return parent::__construct($options);
}
}
Then, you must redefine the default one (in your config.yml for example):
parameters:
session.storage.native.class: ElaoBackBundleSessionStorage
services:
session.storage.native:
class: %session.storage.native.class%
arguments: [%session.storage.options%, @service_container]
这篇关于如何使用symfony2正确地重新打开会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!