Symfony 3: How to share the cookie on subdomain?

倾然丶 夕夏残阳落幕 提交于 2019-12-12 11:34:02

问题


i have over two subdomains in my site. such as:www.example.com, login.example.com, user.example.com, cart.example.com... i setup the cookie_domain is .example.com in config.yml and php.ini when i setCookies('test', 'value', '.example.com'), but the cookie is always not shared in the subdomain. so, how to do now?

there is my config.yml

session:
    handler_id:  session.handler.native_file
    save_path:   "%kernel.root_dir%/../var/sessions/"
    cookie_domain: .example.com
    cookie_lifetime: 0
    name: TESTSESSIONID

in my security:

security:
    session_fixation_strategy:  none

回答1:


You can configure the session key in the config.yml defining a cookie_domain. As example:

config.yml

session:
    cookie_lifetime: 0
    save_path: %kernel.root_dir%/var/sessions
    cookie_domain: .my-domain.com
    name: SFSESSID

Hope this help




回答2:


You need to set trusted hosts in your application because for security reason symfony application will respond to whitelisted hosts and subdomins. To do this you have couple of ways to fix it

  1. In your config.yml set trusted_hosts like below
#app/config/config.yml
framework:
    trusted_hosts:  ['example.com', 'login.example.com', 'user.example.com', 'cart.example.com']
  1. You can also set the trusted hosts in the front controller using the Request::setTrustedHosts() method like below.
//web/app.php 
Request::setTrustedHosts(array('.*\.?example.com$'));

Please find below documentation links are for reference purpose

reference 1

reference 2

reference 3




回答3:


i needed this to work for all subdomains, but the domain itself changed depending on whether i was developing or in production.

I use parameters_dev.yml and parameters.yml to define the 'domain', then added this in config.yml to allow cookies accross all subdomains.

framework:
session:
    cookie_domain: '.%domain%'


来源:https://stackoverflow.com/questions/40124862/symfony-3-how-to-share-the-cookie-on-subdomain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!