2013-05-09 49 views
0

下面的代碼不起作用:PHP登錄使用password_hash

登錄頁面:

if (isset($_POST['login'])) { 
    $name = $mysqli->real_escape_string($_POST['name']); 
    $pass = $_POST['pass']; 
    $query = "SELECT * FROM users WHERE name='{$name}'"; 
    $result = $mysqli->query($query) or die($mysqli->error.__LINE__); 
    if($result->num_rows > 0) { 
    while($row = $result->fetch_assoc()) { 
      if(password_verify($pass, $row['pass'])){ 
       $_SESSION['logged'] = true; 
       $_SESSION['name'] = $row['name']; 
       $_SESSION['pass'] = $row['pass']; 
       //header('Location: index.php'); 
       echo "Workded"; 
      } else { 
       echo "Crypt Not Matching"; 
      } 
     } 
    } 
} 

代碼用於插入到數據庫:

if (empty($e1)) { 
     $password = password_hash($pass, PASSWORD_BCRYPT); 
     if ($mysqli->query("INSERT into users (name, pass, email, gamertag, psnid, youtube, fbauth) VALUES ('$username', '$password', '$email', '$xbox', '$psn', '$youtube', '$fbid')")) { 
      session_start(); 
      $_SESSION['logged'] = true; 
      $_SESSION['name'] = $username; 
      $_SESSION['pass'] = $password; 
      header('Location: index.php'); 
     } 
    } 

任何人都可以指出我什麼做錯了?

+1

有什麼不好?你遇到了什麼錯誤?你會得到什麼?你期望得到什麼? – Tivie 2013-05-09 00:30:32

+0

沒有錯誤,只是想驗證登錄。而將登錄密碼與存儲在數據庫中的登錄密碼相匹配以初始化會話並登錄用戶。 – 2013-05-09 00:31:35

回答

0

1 - 調試$ _ POST var和檢查其是否來自HTTP POST請求

var_dump($_POST); 

2正確傳遞 - 調試數據庫的結果,在foreach循環中,並檢查是否你從db正確檢索信息

while($row = $result->fetch_assoc()) { 
    var_dump($row); 
} 

3 - 測試password_hash和password_verify是否正常工作。

$pwd = 'some_password'; 
$hash = password_hash($pwd, PASSWORD_BCRYPT); 
var_dump(password_verify($pwd, $hash);