Symfony 2 sessions not saved after creation via AJAX call

Let me give you a script. I have an AJAX call that calls / auth - this page, in turn, checks to see if the user is verified and sets a couple of session variables (code below).

$session = $this->getRequest()->getSession(); $session->set('fbid', $fbid); $session->set('name', $name); // not sure if this is even needed - get the same with or without //$session->save(); 

Now, if I go to another page and try to access this session, it returns empty.

If I install these sessions on a regular page (not one of them is accessible via XMLHttpRequest / AJAX), it works fine.

Here are my session settings in config.yml :

 session: cookie_lifetime: 3600 cookie_httponly: false 

I thought the problem could be httponly , but it didn’t.

Any suggestions? Did I miss something?

* UPDATE *

Here are the security / firewall settings:

 firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false login: pattern: ^/demo/secured/login$ security: false secured_area: pattern: ^/demo/secured/ form_login: check_path: /demo/secured/login_check login_path: /demo/secured/login logout: path: /demo/secured/logout target: /demo/ 

I did not change the firewall settings at all, and from what I can say, it should not affect the /auth call through XMLHttpRequest.

* Update 2 *

I ended up adding $session->shutdown() after two sets (and yes, I know that shutdown is not a Session object method). The result was an error message, but because of this, it started working, and it actually saved it in $ _SESSION. So, if this error made it save up to $ _SESSION, there must be an actual method that forces saving. Really strange ..

+4
source share
1 answer

I realized what the problem is.

The first session was created in a production environment (an action called via AJAX / XMLHttpRequest), and the next page on which I tested it was actually in a development environment. At that time, it never occurred to me that Symfony 2 actually saves sessions for prod and dev in different places - PHP usually has a default place where it saves them all.

Symfony 2 overwrites the default value of session.save_path and sets it for each - wasted a day before I found out about it, unfortunately.

Also wrote an article about this, so others do not need to go through the same problem.

http://jondev.net/articles/Reasons_why_Symfony_2_sessions_might_not_always_persist

+9
source

Source: https://habr.com/ru/post/1435957/


All Articles