symfony动态设置cookie_domain [英] symfony set the cookie_domain dynamically
问题描述
我有一个可以服务多个域的应用程序。
我在 framework.session.cookie_domain
I have a single app that ca serve multiple domains.
I'm having a problem with the framework.session.cookie_domain
- 我希望会话保持在子域之间,到目前为止,正确设置cookie_domain
-
我有问题的地方是我想要cookie_domain参数动态设置,因为我事先不知道请求来自哪个域。
- I'd like the session to be kept between subdomain, so far so good with cookie_domain set right
Where i have a problem is that i'd like the cookie_domain parameter set dynamically as i don't know in advance which domain the request is coming from.
- 我在
中尝试过AppKernel.php
做类似的事情:
- I tried in the
AppKernel.php
to do something like :
$ domain = substr($ _SERVER ['HTTP_HOST'],strpos($ _ SERVER ['HTTP_HOST'],'。'));
ini_set('session.cookie_domain',$ domain );
- 但这似乎中断了我的会话
每个域我可以有多个 config.yml
,但我想避免这种情况。
I could have multiple config.yml
one for each domain but i'd like to avoid that.
您知道一种方法吗?
谢谢
推荐答案
好,我已经知道了
那不是那么困难。
我创建了一个自定义sessionStorage,扩展了默认的sessionStorage和i在处理选项的地方做了一个简单的覆盖:在那里我计算了cookie_domain并将其传递给parent :: function:
I created a custom sessionStorage, extending the default one and i did a simple override where the options were being dealt with: there i calculated my cookie_domain and passed it to the parent::function :
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
/**
* DynamicDomainSessionStorage.
*
* @author Julien Devouassoud
*/
class DynamicDomainSessionStorage extends NativeSessionStorage
{
/**
* setOptions.
*
* {@inheritDoc}
*/
public function setOptions(array $options)
{
if(isset($_SERVER['HTTP_HOST'])){
$domain = substr($_SERVER['HTTP_HOST'], strpos($_SERVER['HTTP_HOST'], '.'));
$options["cookie_domain"] = $domain;
}
return parent::setOptions($options);
}
}
不要忘记:
•将您的课程声明为服务
• to declare your class as a service
•将此服务设置为存储空间
• set this service as storage
•设置保存路径,否则cookie_domain似乎不起作用(中断会话)
• set the save_path otherwise cookie_domain seems not to work (breaks the session)
•我也设置了一个名称,但我不认为这是必不可少的
• i set a 'name' as well but i don't think it's essential
•代码 config.yml
:
#...
framework:
#...
session:
storage_id: v3d.session.storage.dynamic_domain
save_path: %kernel.root_dir%/cache/var/sessions
name: SFSESSID
services
v3d.session.storage.dynamic_domain:
class: V3d\Bundle\ApplicationBundle\Services\DynamicDomainSessionStorage
这篇关于symfony动态设置cookie_domain的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!