Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony, jQuery.ajax() call, session variables lost

I am writing a web app using Symfony 1.3. I have a problem with my session variables getting lost after I call an action via jQuery's $.ajax() call. Things happen in this order:

  1. An action sets the session variable like this:

    $this->getUser()->setAttribute('uploaded-files', $uploadedFiles);
    
  2. Then when the action calls the view component, the resulting HTML page contains the following call:

    $.ajax({
      type: "POST",
      url: '<?php echo url_for("content/merge"); ?>',
      cache: false,
      success: mergingCompleteCallback
    });
    
  3. I click on the button that triggers the above ajax call. The call executes the corresponding action method, but when I print out the content of the 'uploaded-files' session variable, it's empty.

    I also checked to see if the session id stays the same between the call to the page that sets the variable and the page that reads the variable (the ajax call) and the session id hasn't changed. I was searching for hours online and I wasn't able to find a solution.

like image 627
pkout Avatar asked Oct 10 '22 03:10

pkout


1 Answers

I had the similar problem, and the issue was that I was using host value for storing cookies with 'www' i.e. www.mydomain.com and I was making Ajax request without 'www'

The solution is use the wildcard like host. Like .mydomain.com as your host to store session cookie on browser end.

Just make sure you are not a victim of this?

like image 72
Humayun Avatar answered Oct 13 '22 20:10

Humayun