Hello. There is a PHP script that I would like to follow during its operation. That is, the function is executed, sends a report, continues to work, and at this time I am watching this report on the html-page.

I decided that I would try to combine PHP with AJAX. It turned out something like this:

ajax.js

function go() { var req = getXmlHttp() var statusElem = document.getElementById('status') req.onreadystatechange = function() { if (req.readyState == 4 && req.status == 200) { statusElem.innerHTML = req.responseText } } req.open('GET', '/script.php', true); req.send(null); statusElem.innerHTML = 'Ожидаю ответа сервера...' } 

script.php

 <?php $somearrays = array('1', '2', '3'); foreach($somearrays as $somearray) { sleep(3); echo $somearray; } ?> 

Without thinking twice, I pressed the button and waited every 3 seconds for a new “report”. But it was not there. After 3 seconds, no “report” came, but for some reason they all came after 9 seconds, which did not make me very happy.

Question: How to get the results of the function execution in a cycle during its operation?

  • Try to explicitly close the session before sleep() (I don’t remember the function, but you will quickly find it). Most likely, it is session blocking. - etki
  • It is necessary to remove the buffer in php and webserver. - zb '
  • In general, I did not fully understand all the answers listed below, but after learning from some that you can run PHP scripts through the command line and monitor its execution, I decided to try it using SSH and it all worked. Thanks for answers. I can not determine which one is the best, let everyone choose for themselves the appropriate answer. - LastChaos

2 answers 2

GET /script.php is not generally called “work tracking.” This is a new process call.
The answer comes only at the end of the web script - this is normal behavior.

As far as I understand the original task, you need to have some long-lived process on the server (CLI, not CGI) and see what it has worked out. I recommend writing the script results to a log file, and reading the log by a web monitor on a timer. That is, we need 3 components:

  • CLI-script itself, writing to the log;
  • the server part of the monitor that can read the log from the specified location;
  • client monitor that sends the job to the server and visualizes the response.

Google a combination of "php web tail".

    If we talk about the problem "spits it all out right after the script is finished," then it is known and solved simply:

    1. output_buffering to 0
    2. Check if it is set.
    3. Before this code add ob_implicit_flush(true);

    I just don’t understand why there’s a need for AJAX, and why it’s impossible to access the script directly.

    But if we talk about this particular case, the very idea of ​​monitoring long-running scripts through the browser is not too successful, to say the least.

    Learn to use standard web programmer tools - console and script launch in CLI mode. Here you will be logged, and independent of the whims of browsers, and convenient output, and simple translation of these scripts into cron and another million reasons why no web programmer in his right mind is engaged in such “monitoring” through the browser.

    • Thank you for the detailed answer, but I don’t quite understand this logic: run PHP scripts "on myself" through the command line and at the same time interact with the site on the server. Can you briefly explain or direct to any sensible guide? - LastChaos