Show Results While Script Is Still Executing

Show results while script is still executing

You can use output buffering like this:

ob_start();

echo('doing something...');

// send to browser
ob_flush();

// ... do long running stuff
echo('still going...');

ob_flush();

echo('done.');
ob_end_flush();

PHP show results while running

I used this way from this answer

while(1) {
echo "should display these lines on browser while in infinite loop.<br>";
flush();
}

or using the for loop, they both work fine, and it to make it more accurate i use ob_flush() with flush()

for($i=0; $i<5000; $i++) {
echo "should display these lines on browser while in infinite loop.<br>";
usleep(30000);
ob_flush();
flush();
}

they both work fine without ajax

how to show results while a php script is still running

Try adding a call to flush() after the call to ob_flush(). This will only work if your server is configured to be able to do it, and does not guarantee that the client will handle it sensibly, but it is your best bet.

One sticking point I have come across here is that is you have zlib.output_compression configured you absolutely cannot do this, full stop. The zlib output compression process is started before any of your code is executed and cannot be controlled by your script at run time with ini_set() and the like.

Echo messages while php script still executes

So, this is a old post but I found a solution for this. As I also have to make the same thing, output when the script is still running. Not any answer from here helped.
First of all, I am using Win32 server(production) and XAMPP as local for tests. This example is just a proof of concept and can be modified as you please.

<?php 
ob_implicit_flush(true);
for($i=1; $i<=10; $i++){
echo "$i ...<br>";
for($k = 0; $k < 40000; $k++) echo ' ';
sleep(1);
}
?>

So, we open output buffer as implicit. Then we make a demo loop to count from 1 to 10 and display the values as they are been processed. Second loop will fill in the browsers buffer. And finally to check if everything is working well we make a sleep for 1 second. Otherwise the script will run too fast and we could not know if we achieved the goal.
Hope this helps !

How to get partial output when script is executing?

Yes, it should be achieveable with PHP alone. I remember seeing some code related to this when I was tracing through wordpress a while back, when it updates modules, it has to ftp, unzip, copy files, etc, which take a long time, and it likes to keep the user updated with what it's doing at the moment... after inspecting their send_message() function, and reading the PHP ob_flush() page, i think you want:

echo "stuff\n"; // adding a newline may be important here,
// a lot of io routines use some variant of get_line()
ob_end_flush(); // to get php's internal buffers out into the operating system
flush(); // to tell the operating system to flush it's buffers to the user.

The people at the php manual also implied that it might help to explicitly set a header() with the mime-type and character set in it, so the browser would know that at the start and wouldn't wait for the entire binary object to become available before attempting to decypher what sort of entity it had.

If that doesn't work you'd need to further modify your system php.ini to turn output buffering and compression off, at which point you may as well go look at an ajax solution.

An ajax solution would look something like:

Your script spits out some html/javascript to hit an ajax request, flushes all it's buffers, and stops concerning itself with the user, then starts some operation, or perhaps merely indicates that some large operation is to commence in a database somewhere for a cron job to pick up. The javascript on your page would have a timer loop to poll an ajax endpoint for status until the ajax replied that it was complete. The ajax endpoint would check on the status of your task by querying the database, or examining output files, etc, and spit all it knew out at once and terminate, letting the client decide when to ask it if things were done again. It is a much more involved and complicated endevour with more moving parts, but achieves a very nice end product for the user, if it's worth your time to craft.

PHP - Can't check session while script is executing

According to this post (Using same session ID within two PHP scripts at same time), you cannot execute php scripts simultaneously with the same session id. This is called session lock.

The solution was to only start the session for the write, and to close it immediately after, leaving the session id unused for the CheckProgress to use.

New long script

for($i=0; $i<10; $i++)
{
@session_start();
$_SESSION["pages-processed"] = $i;
session_write_close();
sleep(2);
}

New CheckProgress

@session_start();
if(isset($_SESSION["pages-processed"]))
echo $_SESSION["pages-processed"];
else
echo 0;

Now it works like a charm :)

RStudio scripts don't show any output

You have to select the lines that you want to run! Before clicking the run button, make sure you have selected the lines of code. Maybe it's the problem ;)

Different results running script from command line vs. PHP

Since the script doesn't begin with a shebang line, it's being executed using whatever shell is being used by the caller. That's /bin/sh for PHP, but probably /bin/bash for you.

This is probably changing whether the echo command supports the -en option to process escape sequences -- it works interactively, but not from PHP.

You should always begin scripts with #!/bin/bash or #!/bin/sh to make sure they use the shell you want.

And rather than echo, use printf, which has more consistent behavior.

#!/bin/bash
printf "GET / HTTP/1.1\nHost: www.betoglou.com\nConnection: keep-Alive\n\n" | time /test/openssl/bin/openssl s_client --connect origin.betoglou.com:443 --servername www.betoglou.com -ign_eof



Related Topics



Leave a reply



Submit