简体   繁体   中英

PHP - echo before exec()

Good day!

I am having some issues with getting the echo statement to output before the execution of the exec()

 <?
    if (isset($_POST['ipaddress'])) {
       $escaped_command = escapeshellcmd($_POST['ipaddress']);

       if(filter_var($escaped_command, FILTER_VALIDATE_IP)) {
           echo "Gleaning ARP information, please wait..";
           $command = exec('sudo /sbin/getarp.exp');

The echo statement is being outputted after the execution of the $command. The execution time can be anywhere from 15-30 seconds depending on how large the ARP table on the remote router is. Is there an order of operations that I am not aware of? It appears that all the statements within the if statement are executed in parallel and not by line by line as I had assumed.

I would rather not a solution be provided, but some documentational links that would lead me to finding a solution. I have searched what I could, but was not able to find a viable solution.

Any help would be appreciated.

Thanks.

之所以发生这种情况,是因为该脚本将在将任何结果/输出发送到浏览器之前完整运行。

In PHP there is a concept of "output buffering".

Whenever you output something (eg using echo , print , etc.) the text is thrown into a buffer. This buffer is only sent at certain times (at the end of the request, for instance, or when the buffer is full).

In order to empty the buffer (to "flush" it) you need to do it manually. The flush() function will do this. Sometimes you also need to call ob_flush() (this is if you have opened custom output buffers yourself). It is generally a good idea to just call both functions and be done with it:

echo 'Wait a few seconds...';
flush(); ob_flush();
sleep(3);
echo ' aaand we are done!';

See Output Buffering Control for more information on output buffering in PHP.

This is probably an issue with the output buffer. PHP buffers output and writes it to the browser in chunks. Try adding a call to ob_flush() between the echo and the exec(); this will force PHP to write the current contents of the buffer to the browser.

By default, php does not send any of the output until the php script is done running completely. There is a solution. However, I hear it is a little browser dependent. I would test it on different systems and browsers to see if it is working:

ob_implicit_flush (true)

Put that before any of your echo/print commands and that should allow anything printed to show right up on the browser.

A more universal approach would be to integrate your page with asynchronous javascript. A process commonly referred to as "AJAX". It is a little more difficult because it requires the use of many interacting scripts, some client-side and some server-side. However, AJAX is the defacto way to do thing like this on the web.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM