1

So I have a form that the user fills out, when submitted an new tab opens to load the results - the script iterates through multiple XML files - this can be a time consuming process since a lot is happening in that script (2-10 minutes). I wanted some AJAX that would track the progress of the results page so what I did was I had the results page update the user's session (You may just want to skip to the bottom for my question) Example:

$_SESSION['fileCount'] = $fileCount;
for($i=0;$i<fileCount;$i++){//iterate through xmls
    $_SESSION['curFile'] = $xml[...];
    $_SESSION['curCount'] = $i;
    //Do stuff
}

The form tag on the parent page looks like:

<form action="results/" method="post" onsubmit="openProgressDialog()" target="_blank">

the function above looks like:

    function openProgressDialog(user){
        $('#loadSplash').dialog('open');
        var loading = true;
        var sleep = 50;
        var precent = 0;
        var fileCount = 0;
        var i = 0;
        var curFile = '';
        while(loading){
            $.getJSON('php/getProgress.php?u='+user, function(data) {
                var items = [];
                $.each(data, function(key, val) {
                    if(key == 'fileCount')
                        fileCount = val;
                    else if(key == 'curCount')
                        i = val;
                    else if(key == 'curFile')
                        curFile = val;
                });
                if(fileCount > 0){
                    //Do stuff
                    $('#pregressbar').progressbar('value',(i/fileCount)*100);
                    if(i == fileCount){
                        sleep = 0;
                        loading = false;
                    }
                }
            }).delay(sleep);
        }
    }

And finally the the getProgress.php looks like:

<?php
session_start();
$progress =array('fileCount' =>intval($_SESSION['fileCount']),'curCount' =>intval($_SESSION['curCount']),'curFile'=>$_SESSION['curFile']);
echo json_encode($progress);
?>

So I know I just used an odd combo of pseudo and real code and I'm also aware that the JS function is incomplete - I've been monitoring the NET panel in firebug to verify functionality.

So my issue is that the getProgress.php doesn't return until the results/index.php page completes - which complete defeats the purpose of all this. Is there something about multiple php scripts executing or using the same resources simultaneously that is my hold up here?

I'm running a new version of apache/php on LAMP/Ubuntu.

As a side note/question I'm particularly interested by this topics because I'm planning a multiplayer game that uses AJAX/php/mySQL/long polling and I'm curious if there is anything I should be aware of venturing into that project.

jreed121
  • 2,067
  • 4
  • 34
  • 57

1 Answers1

3

I've asked a similar question once, Show progress for long running PHP script.

The point is, sessions cannot co-exist. You can't open the same session on 2 scripts simultaneously, one would trigger an error.

What you should do is write the status to a database table, and read from that in your AJAX call. Another option would be to use APC Cache.

Community
  • 1
  • 1
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • I kind of thought that, I had originally tried this with a file that I'd just fopen in both the scripts, but it also behaved the same way. I'll give the db table a shot. – jreed121 Apr 10 '12 at 21:01
  • you could accomplish this without db interaction for status updates. you can use php to pass back data to an ajax function with a percentage and then use that to continue the query. – NDBoost Apr 10 '12 at 21:06
  • @Mike, not sure how that would work considering that it's two different scripts trying to access the same info. – jreed121 Apr 10 '12 at 21:10
  • Never mind about the DB solution anyways - I saw a comment on your question suggesting using APC - this is perfect as my app is already using the heck out of APC so this will be a simple solution! – jreed121 Apr 10 '12 at 21:14