du8828 2013-10-20 22:22
浏览 49
已采纳

强制PHP在循环结束时回显

I'm testing PHP5.5's new password_hash technique and, for debug purposes, am wanting to have PHP echo each time my for loop finishes, instead of waiting for the page to finish loading (or for apache to cut it). I am using wamp server for my debugging, and have enabled implicit_flush, and disabled output_buffering.

My code is:

<?php
    ob_start();
    $wordtohash = "rasmuslerdorf";

    //require ircmaxell's PHP5 backward compat lib (https://github.com/ircmaxell/password_compat)
    require 'lib/pw.php';

    echo "Beginning hashing: <br /><br /><hr /><br />";

    ob_flush();

    $options = [
        'cost' => 15
    ];

    for ($i=0; $i < 10; $i++) {
        if (isset($hashed)) {
            $wordtohash = $hashed;
        }

        //start counting
        $mtime = microtime();
        $mtime = explode(" ", $mtime);
        $mtime = $mtime[1] + $mtime[0];
        $start = $mtime;

        //hash
        $hashed = "Attempt {$i}: ".password_hash($wordtohash, PASSWORD_BCRYPT, $options);

        //end counting
        $mtime = microtime();
        $mtime = explode(" ",$mtime);
        $mtime = $mtime[1] + $mtime[0];
        $end = $mtime;
        $totaltime = ($end - $start);
        echo $hashed." (total time: ".$totaltime." seconds)<br /><br />";

        ob_flush();
    }
?>

What this is doing is hashing a test word, "rasmuslerdorf", and collecting the hash and then sending that back in to be hashed (for as many times as the loop specifies).

I have a timer set up for each, but would like to have each show up on the page as they finish for testing.

I have tried using ob_start() followed by ob_flush() commands where needed, but that didn't work (implicit_flush is supposed to make it flush on echo or print, so I'm at a loss) and I have also tried flush().

Does anyone happen to have any extra ideas? Much appreciated.

  • 写回答

1条回答 默认 最新

  • douyou1960 2013-10-20 22:32
    关注

    I would try using PHPs error_log() function:

    $string = $hashed . " (total time: " . $totaltime . " seconds)" ;
    error_log(print_r($string, true);
    

    This will print your debug message to the PHP error log in real time without relying on the view layer of your application or interrupting the execution of your code. On a UNIX / Linux system or OS X you could tail the log. You could do this in windows too if you have Cygwin or another BASH emulator installed:

    tail -f /path/to/your/error.log 
    

    You can use phpinfo() to determine the location of your PHP error log. If you have not specified custom settings for the log in your php.ini PHP will likely default to outputting errors to the Apache log.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作