3

How do you close an ajax request from php before the script ends? Example: user requests php.php, which has the line: echo "phpphp", and after this line, the ajax request finishes and has the data "phpphp", but the PHP script keeps on running, without using processes or forking?

nwalke
  • 3,042
  • 3
  • 29
  • 59
John Smith
  • 111
  • 5
  • uhm... maybe just use [`die()`](http://www.php.net/manual/en/function.die.php) to return the data or after echoing them? – Bjoern Jul 05 '12 at 20:42
  • that doesn't allow the script to keep on running, the point is to return to the browser but continue to run – John Smith Jul 05 '12 at 20:43
  • 2
    check out http://stackoverflow.com/questions/10403521/how-do-i-implement-this-scenario-using-php/10403777 – Musa Jul 05 '12 at 20:43
  • @JohnSmith sry, you but your question could be interpreted in both ways. if you actually _want_ your php script to continue running, it has to use a fork or invoke some other script before stopping. please explain a bit more what you want to archieve with it, so people can explain what solution suits better. – Bjoern Jul 05 '12 at 20:47
  • well this is on a shared host so no forking allowed. the point is to start off a search which takes over 12 seconds, return immediately with a unique id where results will be stored, and to poll the results every 1 second with ajax requests and teh cookie – John Smith Jul 05 '12 at 20:50
  • You're going to poll every second on a script that takes over 12 seconds to run? – nwalke Jul 05 '12 at 20:54
  • 2
    Instead of waiting for an ID to return, you should generate a unique ID beforehand, send it to the user, kill the buffer and send the proper headers first (see Musa's link). This way the user has a unique ID to poll against. – Blake Jul 05 '12 at 20:57
  • 1
    I'd do it asynchronously. Fire off an ajax event which starts the script, then in cyclic intervals fire off other requests for another script which just fetches the results retrieved so far. My first choice would be using a database for it, but you could also use other mechanisms (p.e. session variable). – Bjoern Jul 05 '12 at 21:01

3 Answers3

3

How do i implement this scenario using PHP? has the answer. Set Connection close and Content length headers, with the flushing.

  ob_start();

  echo "111";
  header("Content-Length: ".ob_get_length());
  header("Connection: close");

  flush();
  somescript();
Community
  • 1
  • 1
John Smith
  • 111
  • 5
  • 1
    This doesnt solve it for me. In the scenario where the browser has to update the page using another request(for example Yii's CGridView), the browser's next request has to wait until the whole script has finished execution. It doesnt matter which url is requested in the second request. (WAMP, Windows 7) – Umair Khan Oct 05 '12 at 06:24
  • This doesn't work for AJAX requests. An AJAX requests read the responseText only when the readyState changes to '4'. Although the response headers will be sent once you flush, you will notice that the responseText is only detected once the backend script stops executing. – om_deshpande Jul 31 '13 at 05:59
0

Try streaming the PHP file, and once it hits a certain point, send a return to JS and that will close the JS connection and (perhaps?) allow this php file to continue?

Haven't tried it but it's worth a shot.

// set as plain text
header('content-type:text/plain');

// let it stream
ob_implicit_flush(true);
ob_end_flush();
M.W. Felker
  • 4,335
  • 1
  • 17
  • 18
-1

exit or any alias of exit will stop script execution.

Take a look at output buffering - using ob_start() with ob_flush() or simply flush() might do the trick.

Jason McCreary
  • 66,624
  • 20
  • 123
  • 167
  • This works without using the ajax, on a windows installation, but it doesnt work when the ajax requests the script – John Smith Jul 05 '12 at 21:15