1

この記事を使用して phpass を機能させようとしています: https://sunnyis.me/blog/secure-passwords

新しいユーザーを作成しても問題ありません。すべてがデータベースにアップロードされ、次のような塩漬けのハッシュが得られます。

$2a$08$5i8TUw7Ego09blkDF6Fv.OVGyKMZjLJ7HzSfRZpX62EbsrcxhLbKK

しかし問題は、アカウントにログインしようとするときの検証です。

    <?php
    ini_set('display_errors', 1);
    error_reporting(E_ALL ^ E_NOTICE);

    require("passwordhash.php");

    if (isset($_POST['submit'])){

    if(!empty($_POST['email']) && !empty($_POST['pass'])) {


    $email=($_POST['email']);
    $password = $_POST["pass"];

/* Getting the correct matching email account for login */

    $query="SELECT * FROM user WHERE email='$email'";
    $result = $mysqli->query($query); 
    $numrows=mysqli_num_rows($result);

    if($numrows!=0){ 

    if (strlen($password) < 72) { 

/* Here is the code for retreiving the hash from database using SELECT and then trying to match it with the $password earlier in the code */

    $hasher = new PasswordHash(8, false);
    $stored_hash = "*";
    $query = "SELECT pass FROM user WHERE email='$email'";

    if($row = $result->fetch_array()) { 
            $stored_hash = $row['pass'];
            }

    $check = $hasher->CheckPassword($password, $stored_hash);

    /* at this if statement it never goes through */
    if ($check) {


    $_SESSION['email'] = $_POST['email'];

    header('Location: '. $_SERVER["REQUEST_URI"]);
    die;

    }

/* It always ends up in this else statement*/

    else {
    $feedback="Invalid username or password! $stored_hash $password";

    }

    } else  {
      $feedback="Password must be 72 characters or less";
    }
    } else {
      $feedback="Invalid username or password!";
    }
    } else {
      $feedback="All fields are required!";
    }
    }

    echo "$feedback";

    ?>

常に終了するelseステートメントで、関数に送信した変数に何が含まれているかを確認しようとしました。$passwords には、フィールドにパスワードとして書き込んだものが含まれており、$stored_hash はデータベースから完全にハッシュ化されたパスワードを意図したとおりに取得します。

これは機能です:

function CheckPassword($password, $stored_hash)
    {
        $hash = $this->crypt_private($password, $stored_hash);
        if ($hash[0] == '*')
            $hash = crypt($password, $stored_hash);

        return $hash == $stored_hash;
    }

したがって、私の問題は、変数 $check が空の文字列を返すため、何かが正しく機能していないことです。PHP のバージョン (5.3.23) に何か問題がある可能性はありますか? または、コードに問題がある可能性はありますか?

4

0 に答える 0