Ask your Symfony questions! Pay money and get answers fast! (more info)

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

  • SOLVED

[[LINK href="http://www.wpquestions.com/"]]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:

mysql.ourDomain.com

groovy.ourDomain.com

rubyonrails.ourDomain.com

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)

2010-02-21

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 ".ourdomain.com" (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.

2010-02-21

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
storage:
class: sfPdoSessionStorage
param:
session_cookie_domain: .domain.com
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
);

2010-02-21

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
storage:
class: sfPdoSessionStorage
param:
session_cookie_domain: '.symfonyexperts.com'


This information can be found right [[LINK href="http://www.symfony-project.org/reference/1_4/en/05-Factories#chapter_05_sub_session_set_cookie_params_parameters"]]here[[/LINK]].