我熟悉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));
究竟是什麼錯誤 – Mihai
正在進入'else {echo「error」; '聲明。 –