php - output echo'd contents in browser while script is running? - php

Does anyone know how to output stuff I echo to a browser while the script is running?
I have a long loop that I'd like to output a string after every run of the loop, kind of like a progress bar, does anyone know how to do that?

Use the ob_flush() function:
ob_start();
//echo stuff...
ob_flush();
//echo more stuff...
ob_flush();

Related

How to echo text before shell_exec in PHP

Kindly please help me with this PHP problem. PHP file will call python file according to the user request. This python is carrying a task. I want to echo text before execute the python file. Because I want PHP to give alert on what it is going to do.
for your information, i tried a dummy file of python. the code for python is just to sleep(10);
for PHP code, I have tried to use ob_flush;, ob_start(); and all. the code is as following. as for reminder. the algorithm.py is only contain 10second sleep.:
ob_start();
echo "welcome";
ob_flush();
usleep(1);
$output=shell_exec("./$algorithm.py");
even with this code, it only echo text after finishes shell_exec. I have tried to use exec. still the text display is delayed.
Output Buffering can be used.
ob_implicit_flush(true)
ob_start();
echo('Task is being done');
ob_flush();
$output=shell_exec("./$algorithm.py");
echo('Wait...');
ob_flush();
echo('done.');
ob_end_flush();
echo "welcome"."<pre>".$output."</pre>";

php remove output buffering

I am doing a lot of processing at my PHP page and I want to display progress information to the user, for instance, finished 10% 20% and so. What is happening now is that all the data is displayed at once after processing is done, how can I display it right away!
I tried to set comment out output buffer in php.ini and I tried to use flush() after echo statements, not working, any suggestions?
Here is my code:
ob_start();
while ($line = read_file_line("c:/1.txt")) {
$read_lines_count++;
if($read_lines_count % 100 == 0) {
echo "parsed $read_lines_count <br />";
ob_flush();
}
}
First you need to call ob_start() before any code is printed.
Then echo whatever you want
call ob_flush() when you want to show the buffer on screen.
and at the end, a call to ob_end_flush() to end buffering and show output.
Make sure your php.ini has this line uncommented:
output_buffering = On
More Info : http://www.php.net/manual/en/book.outcontrol.php

Php problem eval/html/php

I've been writing a php/html page encoder/decoder... I know it already exists but it's a university project so go on XDDD
I encode the pages that I want to protect let's say hypothetically with base64_encode and when I receive a request of any pages I have a loader that reads the coded page, decrypts it and with eval executes it. The real problems arise when I try to decrypt and execute a mixed php/html page. Obviously eval can't execute html code so my question is do I really become crazy about splitting the page executing the php code and print the html? And also if I include an encoded php or php/html page do I really have to reuse the method up here?
I hope someone can really help me because i have a week left before the deadline and I can't change the project at this point.
chris here the function and the fisrt calling in $param[0] i've got the filename called
function MyInclude($filename)
{
// create the temp file
$temp_filename = "tmp.php";
$handle = fopen($temp_filename , 'w+');
if (!$handle)
die('Error creating temp file');
// write the decrypted data, close the handle
$tmp=file_get_contents($filename);
$data=MCrypt_Decode($tmp,'PFL_EPU_V100_mia');
fwrite($handle,$data );
fclose($handle);
// start output buffering to contain any output the script creates
ob_start();
try {
include($temp_filename);
} catch (Exception $e) {
die('There was an error in the encrypted file, cannot process');
}
// get the output, clear the buffer
$output = ob_get_contents();
ob_end_clean();
//destroy the temp file
unlink($temp_filename);
// now you can output the buffer, if desired:
echo $output;
}
MyInclude($param[0]);
the $param[0] file here
<?php
session_start();
$_SESSION['title']='Home';
MyInclude("header.php");
?>
<body>
sono il body <?php echo APP_PATH; ?>
</body>
<?
echo "boss";
MyInclude("footer.php");
?>
any idea about it??? or you need some other code??? let me know T_T
Mike
You can eval() a string that contains mixed html and php, just so long as the tags are included.
http://php.net/manual/en/function.eval.php
When eval() encounters a php close tag (?>), it will stop trying to treat it as php code and just echo everything out until it comes to a php open tag.
The typical solution to your problem is something like this:
$file = ... //Your decoded php/html code here
$file = '?>' . $file; //Add a close tag to the beginning;
ob_start();
eval($file);
$output = ob_get_clean();
echo $output; //Or do something else with it... really, if you're
//just going to be echoing it you can skip the output buffering
Is it possible to decrypt the page, write it to a file, then include it? That would let the PHP interpreter do what it does best - interpret PHP documents. That will include HTML/PHP combinations without relying on eval.
The outline of that would be:
// create the temp file
$temp_filename = "tmp.php";
$handle = fopen($filename , 'w');
if (!$handle)
die('Error creating temp file');
// write the decrypted data, close the handle
fwrite($handle, $decrypted_data);
fclose($handle);
// start output buffering to contain any output the script creates
ob_start();
try {
include_once($temp_filename);
} catch (Exception $e) {
die('There was an error in the encrypted file, cannot process');
}
// get the output, clear the buffer
$output = ob_get_contents();
ob_end_clean();
//destroy the temp file
unlink($temp_filename);
// now you can output the buffer, if desired:
echo $output;
Function references
fopen: http://us2.php.net/manual/en/function.fopen.php
fwrite: http://us2.php.net/manual/en/function.fwrite.php
fclose: http://us2.php.net/manual/en/function.fclose.php
ob_start: http://us2.php.net/manual/en/function.ob-start.php
ob_get_contents: http://us2.php.net/manual/en/function.ob-get-contents.php
ob_end_clean: http://us2.php.net/manual/en/function.ob-end-clean.php
unlink: http://www.php.net/manual/en/function.unlink.php
You will need dump the decoded file to another file and include(); it. The eval approach will not work because it will exit with a parse error if the first item in the file is not either an opening <?php tag, or a valid bit of PHP code.
More than this, you will need to find/replace any occurences of include(), require(), include_once(), and require_once() within the encrypted file with a different function, to ensure you don't try to execute another encrypted file before it has been decrypted. You could do this at execution (ie decryption) time, but it would be much better to it a encryption time, to minimise the time required to pre-fetch the code before it is executed.
You can define these customised functions to decrypt a file and include/require it in your loader script.
Your problem description is a bit vague however your problem seems to be solvable with output buffering.
Have you tried decrypting the page, then parsing the text to split out anything between and then only executing that code?

PHP output to command line

I start my script from command line and it outputs things as they happen but a week ago it stopped outputing and now outputs everything when script finishes. I have ob_start() but as I know this does not effect command line output.
An easy way to do it is to create a function in php like this:
function console_log($message) {
$STDERR = fopen("php://stderr", "w");
fwrite($STDERR, "\n".$message."\n\n");
fclose($STDERR);
}
where $message is the desired output to command line. Then simply call the function wherever you would like to output and pass in whatever you want it to print.
You need to remove ob_start()... try this code on the command line, and it will print the text all at once:
<?
ob_start();
echo "test\n";
sleep(10);
echo "buffer\n";
?>
It'd be very helpful if you could post your script here, at least the relevant parts, but things I'd test are:
Did you turn on buffering?
Are you running the process in something like a nohup or something else that may be buffering it?
Did you change any other buffering settings?
Outputting only at the end of the script seems a buffering problem.

Output buffering in PHP?

I seem to be confused about PHP output buffering. I have code like this:
function return_json($obj) {
ob_get_clean();
ob_start();
header("Content-Type: application/json");
echo json_encode($obj);
exit;
}
But it doesn't seem to like the ob_get_clean(). I do that because some HTML might accidentally get generated before it gets to that point but I thought this was how you were meant to do it.
What am I missing?
To use ob_get_clean (), you have to be sure, that at some point you have ob_start ()'ed earlier. Otherwise, there’s no buffer to clean, everything is already flushed to the user agent.
Use the ob_get_level() function to see if an output buffer is active and quit it:
while (ob_get_level()) {
ob_end_clean();
}
you have to do an ob_start before all your code to catch any output before that function is called
If you just want to clean the buffer after starting output buffering with
ob_start()
use
ob_clean()
Also be aware that nothing is already being flushed with functions like echo, print_r, etc. So the first thing in your script should be ob_start(). Be sure your includes do not already send something to the browser.
ob_start needs to be called before any content is generated. Normal usage would be something like:
ob_start();
# generated content here
$content = ob_get_contents(); # $content now contains anything that has been output already
ob_end_clean();
# generate any headers you need
echo $content;
If the problem you are having is that nothing is going to output, you seem to be missing the flush method? Also, ob_end_clean() can only be called after output buffering has been started, otherwise it returns 'false'. You can't use the ob_ methods to clean up any existing headers that have already been issued, you need to make sure of that yourself.
function return_json($obj) {
ob_start();
header("Content-Type: application/json");
echo json_encode($obj);
ob_end_flush();
exit;
}

Categories