PHP Session Variables and AJAX

This one is a beauty… 

I have recently been writing a web application that combined AJAX and PHP Session Variables.  I soon encountered a problem where it appeared that the session variables were not available form the code loaded by the AJAX call.

The application basically fires off an AJAX call to load a PHP page when a link is selected, and then populates a <DIV> on the same page with the parsed results of that PHP page.  Session variables were being used to store some basic ifnromation about the logged in user – user name, etc. – and I was hoping to make use of these in the AJAX loaded code.

After some serious head scratching it became clear that the PHP session variables, whilst being available everywhere else in the application, were not available from any code loaded with AJAX.

Now, whether this is a known issue or not, or whether I’m being dim, I’m not entirely sure and would welcome any input people have to offer.  However, in the meantime I’ve ended up passing over a couple of extra parameters with the AJAX GET statement and have used those to regenerate the user related data within the AJAX loaded code. 

Because the application is OO designed, this wasn’t a major problem, but actually recognising the problem was a bit time consuming; hopefully this post will save someone else the head-scratching!

17 thoughts on “PHP Session Variables and AJAX

  1. I’ve had this problem, too. After hours of searching and only finding this description of the same problem – with no solution – I had a flash of inspiration. The PHP file that runs on the server did not include the session_start() function.

    Putting it in seems to cure the problem. It seems obvious now.

  2. Cheers Guy,

    Oddly enough I tried this and got nowhere – I expected that this was the problem….

    Having said that, I remember taht the server involved ‘had it’s moments’ so I’m wondering now whether there might have been something unusual in it’s configuration. I guess in the interests of scientific research I should try this baby out on another server!

  3. Hi Wes,

    Unfortunately not – what I did do was replace the use of the GET parameters with a JSON call. That allowed me to do things ‘properly’, albeit with overhead serialising and unserialising the parameters form JSON format.

    I only did this when I started throwing around about half a dozen prameters; by that time the GET string was looking a bit messy so I decided to try and do it right!

  4. Hi Guy,
    Your solution (from June 12, 2008 at 9:50 am:-) solved my problem too, thank you.

    Dan

  5. The reason for this is the fact that in ajax the javascript is loading the class manually, so it acts like its own browser, which means that you do have something like two different browser sessions. because sessions are only persistent during a browser session, the main scirpt’s session isn’t available in the php class called by the ajax script.

  6. Hiya,

    Yeah – a couple of years down the line and I now realise that!! Thanks for chipping in – ironically these days I spend most of my working life fiddling around with PHP / AJAX and Javascript!!

    Cheers,

    Joe

  7. I have found that session variables that are created or updated in a php page are not available to other php pages until after the updating php page is reloaded (or the application unloads the php screen with a new one). I had problems with sharing session variables between php modules where one was in an iframe of the other — as well as in the AJAX calls. You can still access a session variable in php via AJAX calls IF the session variable has not been created or updated by the ‘calling’ php screen. This can be useful when the session variable doesn’t change between screens (e.g. the user’s id number). If I need values that are created or updated by the ‘calling’ php screen, I will pass them in the POST of the AJAX call (or GET as Joe suggests).

  8. I was hoping, Joe, that you would put something here for the dumb-asses that are us! I have the same problem and even session_start() doesn’t seem to help much! How can I possibly pull the results from a MySQL table in an external PHP file using ajax calls? I am trying to log the user in and collect all their information too! It’s 2011, now. So hopefully someone will help 🙂
    Regards

  9. Having said that, it actually worked when I did the sessions right! I had uploaded the wrong script and so I couldn’t see the difference that session_start() made on the php page which was being called by AJAX.
    Now it works! Like a charm.
    Thank you fellow! And the JOE!

  10. I had exactly the same problem trying to work out why session variables weren’t available to my php script and also blamed it on Ajax, then after reading this page I realized I’d forgotten to add session_start() to my php script as well. Thanks Joe, you saved me a nasty headache!

  11. Problem involving the Ajax call being a different browser session:

    Just renember passing the session_id with the AJAX call, so you can resume that specific session within the PHP-script and all should be fine with getting and setting !

    To hint you in the right direction:
    AjaxScript.php?
    session_start(); then uses that session….

    Cheers

  12. I had this problem too. And it cost me a day to solve! The solution came from the following advice from someone on another forum*, but I thought I would share it here in case it helps anyone. For me the problem was the second point. Here’s the advice:

    “I also use PHP and Sessions, and ajax. No problems at all. My magic coins?
    – initializing the session on the first page you come to
    – session_write_close()
    – DB backed sessions”

    * Giles on http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions

  13. Well, another 2 years later, but also had the exact same problem, but David’s session_write_close() at the end of the origination page did the trick. thx!

  14. Ended up finding that for whatever reason my code worked on most servers, but this one hosting company must’ve had their server configuration set up differently. Ended up putting an empty php.ini in the same directory as the ajax file being called and it started working. I have absolutely no idea why this would work, or what kind of voodoo magic is being it because it’s so counter-intuitive but it did the job.

Leave a Reply

Your email address will not be published. Required fields are marked *