Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share the cookie with subdomains

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 as .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.

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
like image 508
Mike Zhang Avatar asked Oct 18 '22 00:10

Mike Zhang


2 Answers

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%'
like image 140
Mike Skinner Avatar answered Oct 21 '22 04:10

Mike Skinner


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

like image 28
Matteo Avatar answered Oct 21 '22 04:10

Matteo