0

I am running one php crawl script in my localhost.

When users fill the form and submit the button, it will post some data to php script(i.e script.php). And it is working right on my localhost, when script.php is running, it shows some information about crawling.

But when I uploaded it to server, the script.php was not shown while processing, only was shown after finished. So I run this below code on server, but also not working (it is working right on localhost).

<?php
header( 'Content-type: text/html; charset=utf-8' );
echo 'Begin ...<br />';
for( $i = 0 ; $i < 10 ; $i++ )
{
    echo $i . '<br />';
    flush();
    ob_flush();
    sleep(1);
}
echo 'End ...<br />';
?>

I added all flush method, including ob_implicit_flush(true); or ob_start(), but the same. I think there is a problem on server, but cannot indicate exactly. Who can help me?

Patrick Q
  • 6,373
  • 2
  • 25
  • 34
Clarenceli
  • 31
  • 5
  • 1
    HTTP doesn't support partial response. Web Server must always grab whole output and return it to client as one. Making it work asynchronously will require extra calls to web server to get updates. – S3Mi Dec 04 '19 at 15:35
  • Does this answer your question? [Display output in parts in PHP](https://stackoverflow.com/questions/4399549/display-output-in-parts-in-php) – Patrick Q Dec 04 '19 at 15:37
  • @Patrick Q from my experience most web browsers will not start rendering until they got full response. So even if you force web server to return partial data, web browser will still wait for the rest. This will work if there is something else than web browser on the other end though. – S3Mi Dec 04 '19 at 15:40
  • @S3Mi The "Does this answer your question?" is the new automated comment text created when flagging a question as a duplicate. I absolutely hate the change, but that's another discussion. This question is pretty much a duplicate of that one, regardless of whether or not what the OP wants to do is actually achievable (and the answers in the linked question point out the limitations / relative impossibility of it) – Patrick Q Dec 04 '19 at 15:43
  • [link]https://copyrighthousebooks.com/test4.php This is the url for this code, and it is working right on the other server. Is it because the Wrong server setting? – Clarenceli Dec 09 '19 at 05:29

1 Answers1

0

As people have said in the comments http pages expect a full response.

The way to get a stream of updates is to have your PHP update a database or a file as it completes stages in the process and then use ajax requests on a seperate http page to query that updated database (via another PHP script) or the file directly.

Sam Dean
  • 433
  • 7
  • 22
  • Ok, sounds good, But here I just wanna ask why even the simple script ( print the number every second ) is not working on server. I put the code to the other server and it is working right, but not just this server. Why? – Clarenceli Dec 05 '19 at 04:06