Run PHP in an orderly fashion

Asked

Viewed 100 times

2

I have a file .php where I connect via SSH to a server, and execute commands through it. My file is basically this:

$conexao(servidor,usuario,senha,PORTA);

$ssh->exec('COMANDO 1');
echo'SUCESSO AO EXECUTAR COMANDO 1';

$ssh->exec('COMANDO 2');
echo'SUCESSO AO EXECUTAR COMANDO 2';

$ssh->exec('COMANDO 3');
echo'SUCESSO AO EXECUTAR COMANDO 3';

It normally runs the commands, no errors, but PHP runs all the commands, and only after that shows the output of the commands echo, all at once.

I wanted to when executing the COMANDO1, was shown just below the output of the echo, informing that the command worked, and then go to the next command and so on.

1 answer

4


PHP even prints in order, the problem is that usually your browser will caching the answer, only to show the user the answer after receiving it completely.

You can add some headers on the server response, to serve as metadata, in order to inform the browser that it should display whatever the result is once it receives it.

For this usually these two headers are used:

header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

They will inform your browser both that your response is a stream, and that it is not to caching the response. Detail that this stream is only a text, if you are going to send an HTML, your browser will not parse this HTML automatically (even because it has no way to parse an incomplete HTML).

Your code would look something like this:

header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

$ssh->exec('COMANDO 1');
echo'SUCESSO AO EXECUTAR COMANDO 1';
flush(); // para imprimir todo o cache do stdout

$ssh->exec('COMANDO 2');
echo'SUCESSO AO EXECUTAR COMANDO 2';
flush();

$ssh->exec('COMANDO 3');
echo'SUCESSO AO EXECUTAR COMANDO 3';
flush();
  • In the example I mentioned echo as echo 'Success'; only that my real echo is echo '<span class="txtsucesso">Success</span>'; a span with a style, to make the text more beautiful, would have a problem?

  • 1

    The "problem" is that <span> will not be interpreted as HTML, but as any text. If you need to handle this response from the server, you will need some Javascript, such as using EventSource.

  • 1

    Unfortunately I don’t have the tools to test here, but try to delete the header Content-Type: text/event-stream and make sure it still works, maybe that’s enough to interpret the answer as an HTML. I can’t guarantee it will work.

  • 1

    @Paulo, PHP sends as you do echo, is the browser that caches to display only at the end. How are you making this request? You are using a simple redirect, or using AJAX?

  • I was able to make the pieces appear in order, but I had to add a code after the headers which is "ob_end_flush();" what he does?

  • And yes, by removing "header('Content-Type: text/Event-stream');" I can easily use html elements in echo

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.