Page 1 of 1

New Here with a very interesting issue

Posted: Wed Dec 31, 2008 9:49 am
by hawkeye0203
Hello all! I'm new here and I was REALLY hoping someone could help me out a little. I've been programming for 4 years now and have not had anything this frustrating. :banghead:

I'll try to explain as best as possible what's going:

I have a drag & drop page for building newsletters. When an item is dragged and then dropped, it calls an AJAX request to get the full content of the item from the database and display it into a lightbox. The user can then accept or cancel the opperation. Here's where the problem happens... If the user accepts the content, it then does another AJAX call to retrieve the data, set a session variable, and then put it into an XML format for return. The javascript then parses the XML, does misc. functions and puts it into the HTML of the template. Here's the code for generating the XML:

Code: Select all

 
    $_SESSION['savedContent'][$_GET['divReceive']]['InsertID'] = $dataId;  // $dataId is the id of the item pulled out of the database
.... // misc irrelavent code
    header("Content-type: text/xml");
    ob_start();
    print "<?xml version=\"1.0\"?>\n\r";
    print "<response>\n\r";
 
    foreach($response as $type=>$content) {
        print "  <{$type}>\n\r";
        if(is_array($content)){
            foreach($content as $subtype=>$subcontent){
                print "  <{$subtype}>\n\r";
                print "      <![CDATA[\n\r";
                print "      {$subcontent}\n\r";
                print "      ]]>\n\r";
                print "  </{$subtype}>\n\r";
            }
        }else{
            print "      <![CDATA[\n\r";
            print "      {$content}\n\r";
            print "      ]]>\n\r";
        }
        print "  </{$type}>\n\r";
    }
    print "</response>";
 
    echo ob_get_clean();
 
The Problem: The very first time a user drags, drops, and accepts content, the $_SESSION variable shown above does not remain set after the Header() is called. If I take out the header(), it stays set (but then my content doesn't get returned correctly, obviously). I have tested various scenario's over several days and everything is leading me to when header("Content-type: text/xml") is set, it kills the $_SESSION variables set during that particular execution of the script.

The kicker is that every subsequent drag, drop, accept instance works just fine. It's just the VERY FIRST time it doesn't...

The ajax call is hitting the same page as the drag and drop interface itself (index.php), but I have a check to see if it's an ajax call. If it is, include the ajax_functions.php file and exit. I do this so I can make use of my various includes (configurations, db connections, etc.). Shown below:

Code: Select all

 
if(isset($_POST['ajax']) || isset($_GET['ajax'])){
    include 'ajax_functions.php';
    exit;
}else{
    // clear previous template session stores
        // This is so when the page is refreshed or a new newsletter is being created, it resets all the previous session variables so no confusion is caused.
        if(isset($_SESSION['savedContent'])) unset($_SESSION['savedContent']);
}
 
Another weird anomaly is that further down the script above, I have a unique, random id being generated each time the page loads. This ID is changing as well after the first XML return! Also, above where it says it's clearing previous session stores, if I comment this out, the problem disappears completely(!), but the session variables persist after refresh and that's not good. These anomalies should not be happening because the "exit" command is killing the page.

I've stated above that the ajax call is hitting the same page as the drag & drop itself, but I have moved everything onto a separate page completely way from index.php and it still happens.

One question that I can not come up with an answer for is: is the header() bypassing my exit somehow? I've done tests that all lead to no and none of the HTML is being returned either, but still other parts of the script are being executed that should not be.

I really hope someone can help me.

Thank you in advance.

Re: New Here with a very interesting issue

Posted: Thu Jan 01, 2009 5:31 pm
by it2051229
hmm... i usually put my "header()" code at the first line before any other codes in my PHP files that handles ajax requests. Give it a try. Although I have to admit that I'm using Cookie and it works but never tried it for sessions.

Re: New Here with a very interesting issue

Posted: Fri Jan 02, 2009 10:55 am
by hawkeye0203
Thank you for your reply.

I was trying to do cookies as well, but due to other reasons, it just wasn't working how I needed it to.

The reason why the Header() is placed where it is, is because the page that the code is on is a compilation of different functions that get called by other ajax requests. This is only one in several on that page.