我正在测试 PHP5.5 的新password_hash
技术,出于调试目的,我希望每次for
循环结束时都有 PHP 回显,而不是等待页面完成加载(或 apache 剪切它)。我正在使用 wamp 服务器进行调试,并启用了 implicit_flush
和禁用output_buffering
。
我的代码是:
<?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();
}
?>
这样做是散列一个测试词"rasmuslerdorf",并收集散列,然后将其发送回进行散列(循环指定的次数)。
我为每个设置了一个计时器,但希望每个计时器在完成测试时都显示在页面上。
我尝试在需要时使用 ob_start()
后跟ob_flush()
命令,但这不起作用(implicit_flush
应该让它在 echo
或 print
上齐平,所以我不知所措),我也尝试了flush()
.
有人碰巧有什么额外的想法吗?非常感谢。
我会尝试使用PHPs error_log()函数:
$string = $hashed . " (total time: " . $totaltime . " seconds)" ;
error_log(print_r($string, true);
这会将您的调试消息实时打印到 PHP 错误日志中,而不会依赖于应用程序的视图层或中断代码的执行。在UNIX/Linux系统或OS X上,您可以跟踪日志。如果你安装了Cygwin或其他BASH模拟器,你也可以在Windows中执行此操作:
tail -f /path/to/your/error.log
您可以使用 phpinfo() 来确定 PHP 错误日志的位置。如果您没有在 php 中为日志指定自定义设置.ini PHP 可能会默认将错误输出到 Apache 日志中。