我正在尝试将我的代码从 mysql_connect 更改为 PDO,但遇到了一些问题。我是 PHP 新手,有人告诉我 mysql_connect 很旧,所以这是我的代码。我似乎无法验证用户。即使我输入了错误的用户,它也不会输出无效的用户。
$_GET['errorCode'];
$errors = array(
1 => 'Invalid User',
2 => 'Incorrect Password');
if(isset($_GET['errorCode'])){
$code = $_GET['errorCode'];
print isset($errors[$code]) ? $errors[$code] : 'Unknown error';
}
if($_SERVER['REQUEST_METHOD']== 'POST'){
$username = $_POST['username'];
$password = $_POST['password'];
if(isset($_POST['username']) && isset($_POST['password'])){
$stmt = $dbh->prepare("SELECT password, salt FROM user WHERE username = :user_name");
$stmt->execute(array(':user_name' => "$username"));
$stmt->bindParam(':user_name', $username, PDO::PARAM_STR);
if(!$stmt->rowCount() > 0)
{
header('Location: index.php?errorCode=1');
exit;
}
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$hash = hash('sha256', $result['salt'] . hash('sha256', $password) );
if($hash != $result['password']) //incorrect password
{
header('Location: index.php?errorCode=2');
exit;
}
else
{
validateUser(); //sets the session data for this user
Header("Location: index.php");
exit;
}
}
}