Reputation: 4608
I have a Symfony2 app and I have to read the sessions set from another, non-symfony app.
The non-symfony app just set its sessions into $_SESSION
, as usual.
However, when I attempt to read this session, the data isn't there. No matter I do it by
$session = $this->get('request')->getSession();
var_dump($session->all());
or even (I know I shouldn't do this, but anyway)
var_dump($_SESSION);
and this gives me session already started error, and I have no idea why there is error despite I have never started session in the Symfony app. Tells me if this way actually work so that I can look into session_start()
thing.
$session = new Session();
$session->start();
var_dump($session->all());
The PHPSESSID
cookie is set in the Symfony2 app and its value is the same as the cookie set in the non-symfony app, but my Symfony2 app just refuse to read the content of the session. ($session->getName()
returns PHPSESSID
, to be clear)
(To be exact, both apps are under same domain but different subdomains, and I have already set framework.session.domain
correctly in app/config.yml
in Symfony app and called session_set_cookie_params
on the non-symfony app to have the same domain setting to allow sharing session cookie between subdomains i.e. .example.com
)
So how do you read sessions in a Symfony2 app/Controller that is set by a non-symfony app? Thanks.
I am using Symfony 2.1, if this matters.
Upvotes: 1
Views: 3364
Reputation: 13505
You won't be able to use the native Symfony2 session wrapper classes because they read session data from app/cache/{env}/sessions/{session_id}
, and your non-Symfony2 app isn't writing its session data to that location.
You could write a custom session handler in your non-Symfony2 app that writes to that location, or better still you could write a native session handler class in Symfony2 which bypasses the default Symfony2 session read location and gets it from the default PHP session path
EDIT: Since writing this answer there is now a much more elegant solution available in gadbout's post below.
Upvotes: 3
Reputation: 1785
No need to write a custom session handler, the native one from Symfony2 can directly read/write from the default PHP $_SESSION.
In config.yml of your Symfony2 app, add a save_path option as below:
framework:
session:
save_path: ~
Clean the cache, and now your sessions will be saved in the default PHP path instead of the Symfony2 sessions folder. You can now share data, that's what I did to login data between a new Sf2 app and an old Sf1 app.
Upvotes: 8