2

I have found similar questions but none as specific as this one. The following code is a simplified version that reproduces the problem.

<?php 
function addProgressText($texto)
{
  echo '<script type="text/javascript">'; 
  echo 'document.getElementById("mensajesEnProgreso").innerHTML += "'.$texto.'";';  
  echo '</script>';
  flush();
  ob_flush();
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width" />
<title>TEST - Flush for Android</title>
</head>

<body>
  <div id="divEnProgreso">
    <p id="mensajesEnProgreso"></p>
  </div>

<?php
addProgressText("Init...");
sleep(5);
addProgressText("OK<br>Step 1... ");
sleep(5);
addProgressText("OK<br>Step 2... ");
sleep(5);
addProgressText("OK<br>Step 3... ");
sleep(5);
addProgressText("OK<br>FINISHED");
?>
</body>
</html>

The code works as expected (displays the steps one by one) on Chrome, Firefox and IE, but when I open it from an Android browser, the flushes don't work and everything is displayed at once upon completion.

Any hints on the source of the problem? Thanks

Mesié
  • 23
  • 3
  • 2
    This is isn't a PHP problem, the browser probably has some buffering of its own, preventing it from displaying text until it has X characters already outputted. – Madara's Ghost Dec 20 '11 at 12:30
  • I was going to say (like @Truth) did, PHP is operating on the back end and has nothing to do with your browser. those `sleep` statements make me worried on a whole different level though. – Jessedc Dec 20 '11 at 12:32
  • @Jessedc it's probably for the sake of testing, but just out of curiosity, why does it worry you? – Madara's Ghost Dec 20 '11 at 12:35
  • The sleep statements are just meant to illustrate the example, they're just replacing some time-consuming operations. Is there any way to find out about this browser buffering? – Mesié Dec 20 '11 at 12:36
  • By adding the sleep statements you're delaying the completion of the request, so i'm not surprised the Android browser waits until it's got all of the page until it starts reading the javascript placed inline. – Jessedc Dec 20 '11 at 12:41

1 Answers1

0

The reason for that is surely the 4K buffering in the Android browser. The only option you have is to pad the output to 4K as described in this bug report.

fab
  • 1,839
  • 15
  • 21
  • Thanks a lot @fab. I checked the bug report you suggested and it looks like it could be the source of the problem. As for the solution, padding every message to 4K is not very "small data plan friendly", but I can't think of anything else but changing the push model for something else. – Mesié Dec 20 '11 at 13:00