Ask your Symfony questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to carry session info from sub-domain to sub-domain Symfony


[[LINK href=""]]We started with WP Questions[[/LINK]]. Now we've created this site, Symfony Experts. Although we've established 2 standalone sites, we are thinking that, moving forward, we will want to have a single over-arching domain for our sites. Then, potentially, our various sites could live as sub-domains of the larger site. For instance:

Next week I'm going to start working on the code for that future site. Ideally we could have a unified user system, and if you logged in once you could visit any sub-domain, but I'm not sure how to code that. Cookies do not carry from one sub-domain to another. Potentially I could hack something like url_for() so that the SESSION_ID is added to every URL? Has anyone written such a hacked version of url_for()? If so, could you please post the code?

And if the url_for() idea is a bad one, does anyone have a better suggestion?

Answers (3)


Ajai Khattri answers:

Actually you CAN carry cookies between subdomains (and hence preserve sessions across related sites) by setting the domain name used in cookies to "" (note the leading dot which makes this a wildcard). In symfony that's set inside your application's factories.yml file.

Ajai Khattri comments:

Also, you dont need to hack session IDs in URLs (if you want that) - that's just another setting in symfony. I personally dont like session IDs in URLs, so my first answer is probably a better solution.


Noel GUILBERT answers:

As Ajai said, you can share your cookies between subdomains by setting the domain name in your factories.yml file. Best, if you want to use several web servers to host your sites, you should store your sessions datas in a database. The setup is quite easy:

# apps/YOUR_APP/config/factories.yml
class: sfPdoSessionStorage
database: doctrine # the database name defined in your databases.yml file
db_table: session

And then create the session table (for mysql):

CREATE TABLE session (
sess_id char(32) not null primary key,
sess_data LONGTEXT,
sess_time INTEGER


Daniel Lohse answers:

Actually, you'll need to add a dot in front of the desired domain name like so:

# apps/YOUR_APP/config/factories.yml
class: sfPdoSessionStorage
session_cookie_domain: ''

This information can be found right [[LINK href=""]]here[[/LINK]].