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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题