2016-06-20 133 views
0

我已經閱讀了很多關於這個password_hash的文章,並且儘可能多地應用了我所讀到的內容 但是password_verify仍然拒絕認證值,不管我嘗試了多少。 PHP版本5.61.6和SQL版本5.7.9 任何形式的幫助表示讚賞,我已經從大量嘗試串組合php password_verify仍然不能正常工作

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Administrator</title> 
     <meta charset="UTF-8"> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    </head> 
    <body> 
     <?PHP 
      //.......all variables are collected from html form.... 
      $conn = mysqli_connect("localhost", "uname", "pword", "dbname"); 
      mysqli_set_charset($conn, 'utf8'); 
      //.......`SN` column has the unique attribute 
      $sql = "SELECT * FROM Sign_Up WHERE `SN`=$sn"; 
      $result = mysqli_query($conn, $sql); 
      if (mysqli_num_rows($result) > 0) { 
       while ($row = mysqli_fetch_assoc($result)) { 
        $date = date('Y-m-j g:i:s'); 
        //.......idgen is a function previously defined 
        $id = idgen(); 
        //.......prints $id before hashing.... 
        echo $id."<BR>"; 
        $id = password_hash('$id', PASSWORD_DEFAULT); 
        //......string length before storing 
        echo strlen($id)."<BR>"; 
        //......table columns 
        $f = $row["FirstName"]; 
        $l = $row["LastName"]; 
        $bn = $row["BusinessName"]; 
        $ba = $row["BusinessAddress"]; 

        $sq = "INSERT INTO Distributors (`FirstName`, `LastName`, `BusinessName`, `BusinessAddress`) VALUES ('$f', '$l', '$bn', '$ba')"; 
        $res = mysqli_query($conn, $sq); 
       } 
      } 
     ?> 

    </body> 
</html> 

以及覈查的哈希碼耗盡是

<html> 
    <head> 
     <meta charset="UTF-8"> 
    </head> 
    <body> 

     <?PHP 

      $conn = mysqli_connect($servername, $username, $password, $dbname); 
      mysqli_set_charset($conn, 'utf8'); 
      //.....phone number has a unique attribute 
      $sql = "SELECT `ID` FROM Distributors WHERE `PhoneNumber`='number'"; 
      $result = mysqli_query($conn, $sql) or die(mysqli_error($conn)); 
      $result1= mysqli_num_rows($result); 
      $look = mysqli_fetch_array($result)['ID']; 
      print $look."<BR>"; 
      $look = trim($look); 
      print $look."<BR>"; 
      print strlen($look)."<BR>"; 
      //......all print statements yields expected results and hashed password is stored 
      //......in VARCHAR (255)...I also tried CHAR 
      $ver = password_verify('user input data', '$look'); 
       if ($ver) { 

        print "ok"; 
       } 
       else { 
        print "no"; 
       } 


    ?> 

    </body> 
</html> 
+1

您散列字面string' $ id' ...和驗證針對字串'$ look'測試。學習如何使用[PHP字符串](http://php.net/string)。 –

+0

'password_verify('user input data','$ look');'真的嗎?將'用戶輸入數據'的密碼字符串與看起來像'$ look'的哈希值進行比較?!? –

+0

...並且您正在驗證文字字符串'$ look'。 –

回答

0

使用PHP變量沒有任何引號或雙引號中,""

$id = password_hash($id, PASSWORD_DEFAULT);   // no quotes around $id 

$ver = password_verify('user input data', "$look"); // double quotes around $look 

單引號的字符串不被解析,即視爲字面STR當雙引號字符串被解析時,因此變量名稱會用它們的值進行擴展。

+0

我已經嘗試在變量被哈希和變量被驗證並且它仍然不起作用時刪除所有引號 –

+1

抱歉,錯誤的評論。你說得對,我無法告訴你我有多開心。它的報價。我是哈希$ id而不是變量的內容 –

0

當散列使用:

$id = "school"; 
Password_hash($id); 

當驗證使用:

Password_verify($id, $data_from_database);