2016-07-06 99 views
0

我想使用密碼哈希使用phpmysql。問題是密碼_驗證似乎並沒有爲我工作到目前爲止。假設我的密碼在註冊時是'123456789'。我將它存儲在數據庫中使用密碼哈希不工作在PHP mysql

password_hash('123456789', PASSWORD_BCRYPT, array('cost' => 12)); 

然後,當我在登錄字段中輸入'123456789'時,它什麼都不做,失敗。

這裏是我的代碼:

<?php 
     session_start(); 
     include('db.php');   
?> 

<!DOCTYPE html> 

<head> 

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <meta name="viewport" content="width=device-width,initial-scale=1" /> 
    <link rel="stylesheet" type="text/css" href="style.css"/> 

</head> 

<body> 

<p/> 

<?php 

    if(isset($_POST['login']) && $_POST['login'] == 'Login') { 

     $loginEmail = $_POST['loginEmail']; 
     $loginPassword = $_POST['loginPassword']; 

     $sqlLogin = $db->prepare("SELECT * FROM registered_users WHERE email = ?"); 

     $sqlLogin->bind_param("s",$loginEmail); 
     $sqlLogin->execute(); 
     $sqlLogin = $sqlLogin->get_result(); 
     $numrowsLogin = $sqlLogin->num_rows; 

     if($numrowsLogin == 1) { 
      $rowLogin = $sqlLogin->fetch_assoc(); 
      $stored_password = $rowLogin['password']; 

     } 
     if(password_verify($loginPassword, $stored_password)){ 


      header('Location: homepage.php'); 
     }else{ 
      echo 'invalid login'; 
     }  

    }   
?> 


    <form action = "<?php echo $_SERVER['PHP_SELF'];?>" method="POST"> 
     <table style="width:500px">       
      <tr> 
       <td width="30%"><input style="width: 200px; height: 25px; border-radius: 5px;" type="text" name="loginEmail" placeholder = "Email" required/><br/></td> 
      </tr>      
      <tr> 
       <td width="30%"><input style="width: 200px; height: 25px; border-radius: 5px;" type="password" name="loginPassword" placeholder = "Password" required/><br/></td> 
      </tr> 
     </table> 

     <input style="font-weight: bold; width: 70px; height: 25px; border-radius: 5px;" type="submit" name="login" value="Login"/> 
    </form> 

</body> 

</html> 
+0

完成任何基本的調試,如檢查您的查詢是否成功並返回存儲的散列?並且請注意,在表單的'action'中使用'$ _SERVER ['PHP_SELF']'就容易受到XSS攻擊。 –

+0

您需要在任何'header('location:...');'-call'之後添加'exit;',因爲我們此時想停止向瀏覽器輸出內容。 –

+0

是的,對不起,已更新 –

回答

3

@Fred李:謝謝你,爲我工作。我在數據庫中的密碼列長度是50.更新它,現在工作,謝謝你再次! - Bishwaroop Chakraborty的」

如commments討論:

實施例從http://php.net/manual/en/function.password-hash.php

$2y$10$.vGA1O9wmRjrwAVXD98HNOgsNpDczlqm3Jq7KnEd1rVAGv3Fykk1a是60個字符

您的密碼列的長度小於60,這就是問題

太短,您的密碼無聲無息因爲它,你需要在改變列的長度後重新使用新的哈希值。

  • 該手冊說255是一個不錯的選擇。

注:

注重留在關於XSS注入其他意見。

這裏有一些好文章:

和頭後添加exit;。否則,你的代碼可能要繼續執行。

+1

imo,只需使用255的'varchar'字段。成本是額外的一個字節,並且這個錯誤永遠不會發生。 imo,從不在數據庫中使用確切長度的字符/ varchar字段。 –

+1

@RyanVincent正好;我寫了一篇關於手冊的簡短說明,說這是「一個很好的選擇」;-)感謝您的評論順便說一句。 –

+0

對不起,@fred,我的意思是它的OP; - /手指問題 –