简体   繁体   中英

PHP output buffer flush and then clean

I am trying to do this:

Display "a" for 1 second, clear screen display only "b" for 1 second, clear screen display only "c".

This is what I have so far, buts it's not working:

header("Content-type: text/html; charset=utf-8");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");

set_time_limit(0);
ob_implicit_flush(1);

echo "a";

ob_flush();
ob_clean();

sleep(1);

echo "b";

ob_flush();
ob_clean();

sleep(1);

echo "c";

Output buffer doesn't work that way, it's a one way street. What has been passed to the browser has been sent from the server and you don't have access anymore to that data and you don't have any control over the data that the user has already received.

The only way to do this would be to send control characters to clear the screen, but those don't fall into the characters browsers will accept.

In theory you could send \\x08 (backspace), but it will not work on anything else but something that allows using these ASCII control characters. Are you working with a terminal or a graphical browser? The first one might accept, the latter most unlikely ever would.

There is no screen. There is only output that is sent from your server (PHP runs on the server) to the browser.

The ob functions use output buffering. Using this technique, you can buffer the output (result of echoes and such) on the server, and even descard or modify it, before sending it to the client (the browser).

Your understanding of those functions is wrong, as is the way you are using them.

At best, the result could be that 'a' appears first, and 'b' would appear a second later. But there are a couple of issues. First of all, you don't start output buffering at all (using ob_start). Secondly, the server might already send the 'a' to the browser, but the browser will also see that it's only one letter, and that the response is still going on, so it will likely not display it. A half response is usually just an incomplete page, so browsers will also buffer the response they get in order not to display a bunch of garbage on screen. They will in most cases only show the response when it is received completely, or when the connection was broken before that.

So in short, this is not going to work. You will need either JavaScript or a meta redirect to fix this.

In a JavaScript-enabled browser, you might do this (no PHP needed):

<body/>
<script type="text/javascript">
// Get the body
var doc = document.getElementsByTagName('body')[0];
// Set its text.
doc.innerText = 'A';
// Replace it with another text after a 1000 milliseconds.
setTimeout(function(){
  doc.innerText = 'B';
}, 1000);
</script>

I wouldn't rely on PHP code to do this and hope that it is timed correctly. Use the php to build your page and retrieve any data you need, and use javascript or jquery to do that you are trying to do. With javascript/jquery, you can dynamically set the html of a page without refreshing. Take a look over here

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