为什么PHP身份验证工作不正确?
我是PHP的新手。 我有一个简单的认证脚本。它工作不正确:用户“测试”(100%存在于数据库中的表中)无法通过身份验证(错误文本-“找不到用户!”) 使用PHP7,MySQL,连接方式为PDO 需要帮忙吗为什么PHP身份验证工作不正确?,php,authentication,pdo,password-hash,Php,Authentication,Pdo,Password Hash,我是PHP的新手。 我有一个简单的认证脚本。它工作不正确:用户“测试”(100%存在于数据库中的表中)无法通过身份验证(错误文本-“找不到用户!”) 使用PHP7,MySQL,连接方式为PDO 需要帮忙吗 $data = $_POST; // check if button is pressed if (isset($data['enter-auth'])) { // check fileds $errors = array(); if (trim($data['login_aut
$data = $_POST;
// check if button is pressed
if (isset($data['enter-auth'])) {
// check fileds
$errors = array();
if (trim($data['login_auth']) == '' ) {
$errors[] = 'Enter login';
}
if (($data['password_auth']) == '' ) {
$errors[] = 'Enter password';
}
// If all fields are filled, save user's data in vars
$login = $data['login_auth'];
$password = password_hash($data['password_auth'], PASSWORD_DEFAULT);
// ... and look in table
try {
if (empty($errors)) {
// Check if login and password exists in table
$stmt = $pdo->prepare("SELECT count(*) FROM users WHERE login=? AND password=?");
$stmt->execute([$login, $password]);
$count = $stmt->fetchColumn();
// If login and pwd found in table counter will be > 0, so ...
if ($count > 0) {
// ... then we can check if password is correct
if (password_verify($data['password_auth'], $password)) {
// if entered and stored passwords match, user is welcome
$_SESSION['auth_name'] = $data['login_auth'];
echo '<div style="color: green;">Welcome, '.$_SESSION['auth_name'].';
echo '<a href="/a/logout.php">Exit</a>';
header('Location: /a/index.php');
} else {
$errors[] = 'Password is incorrect';
echo '<p id="message">Wrong password!</p>';
}
} else {
$errors[] = 'User not found';
echo '<p id="message">User is not found!</p>';
}
} else {
echo '<div style="color: red;">'.array_shift($errors).'</div>';
}
} catch(PDOException $e) {
echo $e->getMessage();
}
// close condition check if button is pressed
}
$data=$\u POST;
//检查按钮是否按下
如果(isset($data['enter-auth'])){
//检查文件
$errors=array();
if(trim($data['login\u auth'])=''){
$errors[]=“输入登录名”;
}
if($data['password\u auth'])=''){
$errors[]=“输入密码”;
}
//如果所有字段都已填充,则将用户数据保存在VAR中
$login=$data['login_auth'];
$password=password\u hash($data['password\u auth'],password\u DEFAULT);
//…然后看看桌子
试一试{
if(空($errors)){
//检查表中是否存在登录名和密码
$stmt=$pdo->prepare(“从登录名为?且密码为?)的用户中选择计数(*);
$stmt->execute([$login,$password]);
$count=$stmt->fetchColumn();
//若在表计数器中找到的登录名和pwd将大于0,则。。。
如果($count>0){
//…然后我们可以检查密码是否正确
if(密码验证($data['password\u auth'],$password)){
//如果输入的密码和存储的密码匹配,欢迎用户使用
$\会话['auth\u name']=$data['login\u auth'];
回显“欢迎”。$会话['auth_name'];
回声';
标题('Location:/a/index.php');
}否则{
$errors[]=“密码不正确”;
回显“密码错误!
”;
}
}否则{
$errors[]=“未找到用户”;
echo“找不到用户!
”;
}
}否则{
回显“”。数组移位($errors)。“”;
}
}捕获(PDO$e){
echo$e->getMessage();
}
//关闭状态检查按钮是否按下
}
注:
我尝试使用var_dump调试此脚本
若在表中搜索时使用fetchAll(),则接受任何输入的ldin(即使并没有这样的用户)
使用带有调试目的的try/catch构造,我听说在生产环境中,由于安全原因,它被弃用。发现错误,根据 所以,正确的片段是:
try {
if (empty($errors)) {
$stmt = $pdo->prepare("SELECT login, password FROM users WHERE login=?");
$stmt->execute([$login]);
$user = $stmt->fetch();
if ($user && password_verify($data['password_auth'], $user['password'])) {
$_SESSION['auth_name'] = $data['login_auth'];
echo '<div style="color: green;">Welcome, '.$_SESSION['auth_name'].';
echo '<a href="/a/logout.php">Exit</a>';
header('Location: /a/index.php');
} else {
$errors[] = 'Login or password error';
echo '<p id="message-auth">Login or password is incorrect!</p>';
}
} else {
echo '<div style="color: red;">'.array_shift($errors).'</div>';
}
} catch(PDOException $e) {
echo $e->getMessage();
}
试试看{
if(空($errors)){
$stmt=$pdo->prepare(“选择登录名,来自登录名=?”的用户的密码”);
$stmt->execute([$login]);
$user=$stmt->fetch();
如果($user&&password\u verify($data['password\u auth'],$user['password'])){
$\会话['auth\u name']=$data['login\u auth'];
回显“欢迎”。$会话['auth_name'];
回声';
标题('Location:/a/index.php');
}否则{
$errors[]=“登录或密码错误”;
echo“登录或密码不正确!
”;
}
}否则{
回显“”。数组移位($errors)。“”;
}
}捕获(PDO$e){
echo$e->getMessage();
}
您有解析错误您使用的密码\u hash()
不正确。此函数生成一个新的哈希,几乎100%保证它与以前为同一密码生成的任何其他哈希不同。因此,通过检查哈希是否匹配,您将无法在数据库中找到用户。@Funk 49,用ini\u set检查('error\u reporting',E\u ALL);ini设置(“显示错误”,1);并从控制台运行脚本,但除了“headers ready sent…”错误之外,并没有看到任何解析错误,但我认为这不会影响我的问题。。。你能建议正确的方法来发现解析错误吗?谢谢。你可以用谷歌搜索那个错误,然后在堆栈上看这里;这种输出是有原因的。@funkforniner,我已经处理了著名的“headers ready sent..”错误,上个月在谷歌上搜索了它。在这个脚本中,我通过在auth脚本中搜索“session start()”并仅在dbconnect.php文件中使用来解决它,这是我所有文件中所必需的。所以现在我在使用ini_set和console命令#php script.php时看到了0个错误。。。