3

I'm having a problem with Symfony creating a new session on each page load, rather than carrying data across requests. The auto_start in the session section in the config.yml is set to false, and regular php sessions work fine. It's only when running in symfony that I get the problem.

For example, I created the test action:

public function sessionTestAction()
{

    $s_Response = '<html><head></head><body><p>Foo</p></body></html>'; //Initialize response and headers
    $a_Headers = array();
    $i_StatusCode = 200;


    $oSession = $this->get('session');
    var_dump($oSession->all());

    if(!$oSession->has('Test'))
    {
        $oSession->set('Test', 'Bar');
    }

    $oSession->save();
    return new Response($s_Response, $i_StatusCode, $a_Headers);
}

The expected action is, that on the first page load, the var_dump will yield nothing, and that on any subsequent executions, it will contain Test=>Bar. However, it never gets that data across requests.

In addition, it creates a new session id for each request.

I am using Symfony v2.0.15, and PHP v5.4

Anyone have any ideas?

Edit:

I made some progress, I think. I made the following changes to the test action:

public function sessionTestAction()
{

     //Initialize response and headers
    $oRequest = $this->get('request');
    $a_Headers = array();
    if (isset($oRequest->headers->all()['cookie']))
    {
        $a_Headers['Set-Cookie'] = $oRequest->headers->all()['cookie'];
    }
    $i_StatusCode = 200;


    $oSession = $oRequest->getSession();
    $oSession->start();
    $s_Response = print_r($oSession->all(), true);
    if(!$oSession->has('Test'))
    {
        $oSession->set('Test', 'Bar');
    }

    $oSession->save();
    $oResponse = new Response($s_Response, $i_StatusCode, $a_Headers);
    return $this->render('Bundle:Default:index.html.twig', array('response' => $s_Response), $oResponse);
}

Where that twig file has just {{response|raw}}. It now holds the session for 2 out of 3 of the requests. However, on the third request, it's cleared.

2 Answers 2

2

Turned out the problem was, someone added a line to set a session cookie whenever the app.php was run, not knowing that symfony handled sessions itself, I guess. Problem solved.

Sign up to request clarification or add additional context in comments.

5 Comments

Show that line please. It's missing code here and not really clear where that takes place.
It was basically just session_start(); added to the ./web/app.php file.
Well, that will start a session for all requests which is not recommended at all. It might have "solved" your problem to not getting the session started, however, it introduces a lot of side-effects you don't want to have. Instead find out why the session didn't start (enable PHP error logging, check for header errors for example, use xdebug to see what happens etc.) and then just fix it at that single place that needs fixing.
@hakre I should probably clarify that I was coming in trying to fix an app that someone else (who had no symfony experience) broke. They were the ones who added that line to the app.php file, trying to fix something, and just ended up making things worse.
I'd say that's worth the comment as future users have more context then to see if that solution is applicable to them or not.
2

I got this problem a couple times, its very annoying. So, let me describe possible solution.

Open dev environment - yourdomain.com/app_dev.php/ Try to refresh page a couple times. If you see session ID changed each time - it means that sessions are broken.

Session id

If you are using chrome (if not - you should, its the best for developers ;) ) - you can open developers tools (click F12). Next, check Network tab, refresh page and locate your main request. Check headers for your request - if should see "Cookie:PHPSESSID". enter image description here

If you dont see - something wrong with cookies. In my case it was

framework:
    session:         
        cookie_domain: mydomain.com

2 Comments

just got this bug again after I have installed some php module (not sure which one exactly). Session's folder wasnt writable for nginx (it wasnt pointed to /tmp for some reasons).
I was wrong about id showed in developer's toolbar - thats debug token, not session id. So, its ok if it changed on each request - it doesnt mean, that sessions are broken (at least for symfony 2.3.*)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.