Вопрос

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.

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top