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?

8
  • uhm... maybe just use die() to return the data or after echoing them? Commented Jul 5, 2012 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 Commented Jul 5, 2012 at 20:43
  • 2
    check out stackoverflow.com/questions/10403521/… Commented Jul 5, 2012 at 20:43
  • 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. Commented Jul 5, 2012 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). Commented Jul 5, 2012 at 21:01

3 Answers 3

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();
Sign up to request clarification or add additional context in comments.

2 Comments

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)
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.
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();

Comments

-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.

1 Comment

This works without using the ajax, on a windows installation, but it doesnt work when the ajax requests the script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.