1

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";
6
  • 1
    I don't think server side programming is done to create ascii gifs. Commented Oct 24, 2014 at 20:16
  • Which of the functions you're using there do you think should clear the screen? Commented Oct 24, 2014 at 20:17
  • I was under the impression that ob_clean(); cleared the screen. Commented Oct 24, 2014 at 20:18
  • This has nothing to do with ascii gifs? Commented Oct 24, 2014 at 20:18
  • @scottpaterson try with ajax. Commented Oct 24, 2014 at 20:20

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your explanation. This would be for a graphical browser.
Then you are completely out of luck if you wish to clear the screen AND can send only one character. You will need something that runs on the browser rather than server for it or a chain of page refreshes with a meta tag en.wikipedia.org/wiki/Meta_refresh
2

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>

4 Comments

Well, in theory, in a highly unlikely corner case when the content type is text/plain and the recipient allows ASCII control characters, this would be doable. But the chances for that being asked are close to nil and if it was the case, I'd suspect that it would be a case where it would be someone who wouldn't have needed to ask this in the first place... :)
If you have a browser that supports control characters, yes. But I think they are less common than the ones which support JavaScript. ;)
Oh I don't even know if those exists in the wild that would accept control chars. :D It's really a highly unlikely theoretical corner case where it would be possible to clear the previously sent character with output buffering.
Afraid so. I think not even Lynx supports them.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.