PHP ob_flush没有在foreach上工作

时间:2016-11-20 02:42:04

标签: php foreach

我编写了一个使用foreach执行某些操作的脚本,它工作正常,唯一的问题是它等到所有操作结束才显示输出,我想要的是实时显示输出,虽然脚本仍在运行。

我曾在this question上尝试过这些建议,但这对我不起作用。以下是我的剧本。

<form name="dataform" id="dataform" method="POST">
  <textarea name="data" id="data" placeholder="username|useremail|userpass" rows="10"></textarea> <br>
  <input type="text" maxlength="1" name="separator" id="separator" value="|">
  <button type="submit" name="iniciar" id="iniciar">Iniciar!</button>
</form>

<?php
if (isset($_POST['iniciar'])) { // Start only if the "iniciar" button is pressed

    ob_start();

    foreach(explode("\n", $_POST['data']) as $line) {

      // Get Separator, Explode and set variables
      $explode = explode($_POST['separator'], $line);

      // Remove white spaces
      $explode = preg_replace('/\s+/', '', $explode);

      $username = $explode[0]; 
      $useremail = $explode[1]; 
      $userpass = $explode[2]; 


      $response = "Success, new user registered!";

      if (strpos($response, "Success") !== false) {
          echo "<b style='color: green;'>Success, new user registered! </b> | ~> " . $username . ' | ' . $useremail . ' | ' . $userpass . "<br>";          
          ob_flush();
          sleep(5); // only for debug purposes
      }

      else {
        echo "<b style='color: red;'>Oh, no! Something went wrong! :(</b>" . "<br>";
      }

    }

}
?>

0 个答案:

没有答案