Question

Using AJAX and PHP, I want to start a long-running PHP script via an AJAX request, and then using using another AJAX request get the progress of the long running script. I have made a test script that simulates what I want. The test as a whole "works" but the initial long-running script produces an error in the browser when it completes. In Chrome I get a net::ERR_RESPONSE_HEADERS_TO_BIG error.

Here is the offending code:

<?php

//Start a long-running process

$goal = 5000;
for($i = 0; $i < $goal; $i++) {
    session_start();  //Start/Reopen session to start/continue updating the progress
    if(!isset($_SESSION['progress'])) {
        $_SESSION['progress'] = $i;
        $_SESSION['goal'] = $goal;
    }
    //wait a wink
    usleep(rand(500, 2000));
    $_SESSION['progress'] = $i;
    session_write_close();  //Close session to gain access to progress from update script
}

?>

I'm pretty sure it has to do with the session being restarted so many times, but I would like some advice. I need to restart the session because I am closing it at the end of the loop to gain access to the session info on another script.

Again, when I put this in with my test, things appear to complete, but I would like to solve the problem that is producing the browser error. I can't reliably test for a successful AJAX request if browser returns an error. Is there a way to limit how much header information PHP can send?

Was it helpful?

Solution

There's probably cookie header generated with every session_start. It's quite a hack, but you may try calling header_remove(); after each session_start();.

OTHER TIPS

Why open and close all the time? Just move the session start outside your loop. I allways put it on top of my file :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top