2015-01-16 46 views
0

我熟悉crypt系統,但我試圖實現一些散列函數,但用戶註冊後我無法登錄。這是哈希類的,我有使用散列函數登錄錯誤

class PassHash { 

// blowfish 
private static $algo = '$2a'; 

// cost parameter 
private static $cost = '$10'; 

// mainly for internal use 
public static function unique_salt() { 
    return substr(sha1(mt_rand()),0,22); 
} 
// this will be used to generate a hash 
public static function hash($password) { 

    return crypt($password, 
       self::$algo . 
       self::$cost . 
       '$' . self::unique_salt()); 

} 
// this will be used to compare a password against a hash 
public static function check_password($hash, $password) { 

    $full_salt = substr($hash, 0, 29); 

    $new_hash = crypt($password, $full_salt); 

    return ($hash == $new_hash); 
} 
} 

這是登錄表單

include 'misc/database.inc.php'; 
require ("misc/hash.php"); 
if(isSet($_POST['submit'])) { 
$pdo = Database::connect(); 
$username=$_POST['username']; 
$password=""; 

$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username LIMIT 1"); //Limiting result to one only. 

$stmt->bindParam(':username', $username); 
$stmt->execute(); 
$res = $stmt -> fetch(PDO::FETCH_OBJ); // Asks to fetch as an object. 

$password = $res->password; 
if (PassHash::checkHash($password, $_POST['password'])) { 


if ($res['level'] == 1) 
{ 
     $_SESSION['firstname'] = $res['firstname']; 
     $_SESSION['lastname'] = $res['lastname']; 
     $_SESSION['email']  = $res['email']; 

    header("location: users/main.php"); 
} 
else 
{ 
     header("location: index.php");    
} 
} else 
{ 
    echo "can't enter"; 
} 

我不斷收到error。同樣的密碼也是正常的(我使用123321進行測試)每次存儲不同的hash或者註冊表有錯誤?

這裏是userAdd文件。

if (!empty($_POST) && isset($_POST['submit'])) 
      {    

      // keep track post values 
      $username= $_POST['username']; 

      $pw = new PassHash(); 
      $myHash = $pw->hash($_POST['password']); 

      $firstname = $_POST['firstname']; 
      $lastname = $_POST['lastname']; 
      $email = $_POST['email'];       


         // update data 

      $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

      $sql = "UPDATE users set username = ?, password = ?, firstname = ?, lastname = ?, email = ?, user_image = ? WHERE user_id = ?"; 
      $q = $pdo->prepare($sql); 
      $q-execute(array($username,$myHash,$firstname,$lastname,$email,$pathForDB,$user_id)); 
+0

究竟是什麼錯誤 – Mihai

+0

正在進入'else {echo「error」; '聲明。 –

回答

2

想想我發現了你的具體錯誤。 你正在發送一個空白的密碼變量來驗證,看看這裏,我評論了我編輯的重要部分。

require ("misc/hash.php"); 
if(isSet($_POST['submit'])) { 
$pdo = Database::connect(); 
$username=$_POST['username']; 
$password=""; 

$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username LIMIT 1"); //Limiting result to one only. 

$stmt->bindParam(':username', $username); 
$stmt->execute(); 
$res = $stmt -> fetch(PDO::FETCH_OBJ); // Asks to fetch as an object. 

// PASSWORD IS STILL EMPTY HERE! 
// Must fill it out, to be the hashed password from database 
$password = $res->password; 

if (PassHash::check_password($password, $_POST['password'])) { 

這是否解決了您的問題?

你可以試試嗎?

class PassHash { 

    protected function salter() { 
     $salt = ""; 
     $salt_chars = array_merge(range('A','Z'), range('a','z'), range(0,9)); 

     for($i=0; $i < 22; $i++) { 
      $salt .= $salt_chars[array_rand($salt_chars)]; 
     } 

     return $salt; 
    } 

     public function hash($input, $rounds = 7) 
     { 
     return crypt($input, sprintf('$2a$%02d$', $rounds) . $this->salter()); 
     } 

     public function checkHash($password, $hash) { 
     if(crypt($password, $hash) == $hash) { 
      return true; 
     } 

     return false; 

     } 
} 


$pw = new PassHash(); 
$myHash = $pw->hash("1234"); 

if($pw->checkHash("1234", $myHash)) { 
    echo "I did it!"; 
} else { 
    echo "I diden't do it!"; 
} 
+0

不管輸入什麼密碼,像這樣保存在數據庫'* 0'的密碼字段中。 –

+0

更新了我的答案。 Testet工作。 –

+0

是的,像這樣工作。但登錄問題仍然存在。只是不登錄..它會進入'else {echo「error」; }'語句 –